Java HTML Jsoup Document getContainersForLink(Document document, String link)

Here you can find the source of getContainersForLink(Document document, String link)

Description

get Containers For Link

License

Open Source License

Declaration

public static List<Element> getContainersForLink(Document document, String link) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Element> getContainersForLink(Document document, String link) {
        List<Element> elements = new ArrayList<>();
        for (Element element : document.body().getAllElements()) {
            if (containsLink(element, link))
                elements.add(element);/*from  w  ww .  j  av a2  s .c o m*/
        }
        return elements;
    }

    public static boolean containsLink(Element element, String link) {
        int links = 0;
        for (Element child : element.children()) {
            if (child.tagName().equals("a") && child.hasAttr("href") && child.attr("href").contains(link))
                links++;
        }
        return links > 0;
    }
}

Related

  1. detectLanguage(Document doc)
  2. emptyDocument()
  3. formatDocument(Document doc)
  4. getAllText(Document document)
  5. getCategoryIds(final Document html)
  6. getDivForClass(Document document, String className)
  7. getDocument(CloseableHttpClient client, String url)
  8. getDocument(final String url)
  9. getDocument(String url)