Java Code Examples for org.jsoup.nodes.Element#getAllElements()

The following examples show how to use org.jsoup.nodes.Element#getAllElements() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LyricsActivity.java    From Refresh-Music-Player with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected String doInBackground(String... params) {
    String song = params[0];
    String artist = params[1];
    String url = "https://www.google.com/search?&q="+artist+"+"+song+"+lyrics+metrolyrics";
    try {
        //To scrap google
        Document googlePage = Jsoup.connect(url).get();
        elem1 = googlePage.getElementsByClass("g").first();
        data = elem1.getElementsByTag("a").first();
        int http=data.toString().indexOf("http");
        int html=data.toString().indexOf("html");
        metroLyrics = data.toString().substring(http,html+4);
        //To scrap metrolyrics
        Document document = Jsoup.connect(metroLyrics).get();
        Elements elem = document.getElementsByClass("verse");
        for (Element item:elem) {
            for (Element j : item.getAllElements()) {
                ans += j.wholeText();
            }
            ans += "\n";
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ans;
}
 
Example 2
Source File: StructuralEvaluator.java    From zongtui-webcrawler with GNU General Public License v2.0 5 votes vote down vote up
public boolean matches(Element root, Element element) {
    for (Element e : element.getAllElements()) {
        if (e != element && evaluator.matches(root, e))
            return true;
    }
    return false;
}
 
Example 3
Source File: StructuralEvaluator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public boolean matches(Element root, Element element) {
    for (Element e : element.getAllElements()) {
        if (e != element && evaluator.matches(root, e))
            return true;
    }
    return false;
}
 
Example 4
Source File: StructuralEvaluator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public boolean matches(Element root, Element element) {
    for (Element e : element.getAllElements()) {
        if (e != element && evaluator.matches(root, e))
            return true;
    }
    return false;
}
 
Example 5
Source File: StructuralEvaluator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public boolean matches(Element root, Element element) {
    for (Element e : element.getAllElements()) {
        if (e != element && evaluator.matches(root, e))
            return true;
    }
    return false;
}
 
Example 6
Source File: DescendantSelector.java    From JsoupXpath with Apache License 2.0 5 votes vote down vote up
@Override
public XValue apply(Elements context) {
    Set<Element> total = new HashSet<>();
    Elements descendant = new Elements();
    for (Element el:context){
        Elements tmp = el.getAllElements();
        //exclude self
        tmp.remove(el);
        total.addAll(tmp);
    }
    descendant.addAll(total);
    return XValue.create(descendant);
}
 
Example 7
Source File: DescendantOrSelfSelector.java    From JsoupXpath with Apache License 2.0 5 votes vote down vote up
@Override
public XValue apply(Elements context) {
    Set<Element> total = new HashSet<>();
    Elements descendant = new Elements();
    for (Element el:context){
        Elements tmp = el.getAllElements();
        total.addAll(tmp);
    }
    descendant.addAll(total);
    return XValue.create(descendant);
}
 
Example 8
Source File: TagServlet.java    From firing-range with Apache License 2.0 5 votes vote down vote up
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  if (request.getParameter("q") == null) {
    Responses.sendError(response, "Missing q parameter", 400);
    return;
  }

  String  q = request.getParameter("q");
  Document doc = Jsoup.parseBodyFragment(q);
  Element body = doc.body();
  Elements elements = body.getAllElements();
  if (!(q.contains("body"))){
    elements.remove(body);
  }

  if (elements.isEmpty()) {
    Responses.sendError(response, "Invalid input, no tags", 400);
    return;
  }

  String allowedTag = "";
  String allowedAttribute = "";
  if (request.getPathInfo() != null) {
    String pathInfo = request.getPathInfo().substring(1);
    if (pathInfo.contains("/")) {
      allowedTag = pathInfo.split("/", 2)[0];
      allowedAttribute = pathInfo.split("/")[1];
    } else {
      allowedTag = pathInfo;
    }      
  }
  handleRequest(elements, response, allowedTag, allowedAttribute);
}
 
Example 9
Source File: StructuralEvaluator.java    From jsoup-learning with MIT License 5 votes vote down vote up
public boolean matches(Element root, Element element) {
    for (Element e : element.getAllElements()) {
        if (e != element && evaluator.matches(root, e))
            return true;
    }
    return false;
}
 
Example 10
Source File: StructuralEvaluator.java    From xsoup with MIT License 5 votes vote down vote up
public boolean matches(Element root, Element element) {
    for (Element e : element.getAllElements()) {
        if (e != element && evaluator.matches(root, e))
            return true;
    }
    return false;
}
 
Example 11
Source File: ElementSelector.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
public ElementSelector(Element element) {
    this.currentElements = element.getAllElements();
}
 
Example 12
Source File: Expression.java    From firing-range with Apache License 2.0 4 votes vote down vote up
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  if (request.getParameter("q") == null) {
    Responses.sendError(response, "Missing q parameter", 400);
    return;
  }

  String  q = request.getParameter("q");
  Document doc = Jsoup.parseBodyFragment(q);
  Element body = doc.body();
  Elements elements = body.getAllElements();
  elements.remove(body);
  if (elements.isEmpty()) {
    Responses.sendError(response, "Invalid input, no tags", 400);
    return;
  }

  StringBuilder res = new StringBuilder();
  for (Element element : elements) {
    boolean validElement = true;

    Attributes attributes = element.attributes();
    for (Attribute attribute : attributes) {
      if (attribute.getKey().toLowerCase().startsWith("on")
          || attribute.getKey().toLowerCase().equals("href")
          || attribute.getKey().toLowerCase().equals("src")) {
        validElement = false;
      }

      if (attribute.getKey().toLowerCase().equals("style")
          && attribute.getValue().toLowerCase().contains("expression")) {
        validElement = false;
      }
    }

    if (validElement) {
      res.append(element.toString());
    }
  }
  Responses.sendXssed(response, res.toString());
}
 
Example 13
Source File: TtsHelper.java    From coolreader with MIT License 4 votes vote down vote up
private void removeAllChildren(Element el, Elements elements) {
	for (Element child : el.getAllElements()) {
		elements.remove(child);
	}
}
 
Example 14
Source File: AxisSelector.java    From CrawlerForReader with Apache License 2.0 2 votes vote down vote up
/**
 * 全部子代节点 儿子,孙子,孙子的儿子...
 *
 * @param e
 * @return
 */
public Elements descendant(Element e) {
    return e.getAllElements();
}
 
Example 15
Source File: AxisSelector.java    From CrawlerForReader with Apache License 2.0 2 votes vote down vote up
/**
 * 全部子代节点和自身
 *
 * @param e
 * @return
 */
public Elements descendantOrSelf(Element e) {
    Elements rs = e.getAllElements();
    rs.add(e);
    return rs;
}