Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get n-th child Element with Jsoup

Tags:

java

html

jsoup

For example a web site has a code like this:

<div>
    <div>
        first
    </div>
    <div>
        second
    </div>
    <div>
        third
    </div>
</div>

and I want to get the "second" div text with "Jsoup" and it has no attribute or class.

like image 548
AmirReza Hosseini Avatar asked Sep 13 '25 15:09

AmirReza Hosseini


1 Answers

There are few ways to do it.

For instance we could use select method which returns Elements with all specified elements. Since Elements extends ArrayList<Element> it inherits all of ArrayList public methods. This means we can use get(index) method to select specific child (starting from 0)

 String html = 
        "<div>\n" +
        "    <div>\n" +
        "        first\n" +
        "    </div>\n" +
        "    <div>\n" +
        "        second\n" +
        "    </div>\n" +
        "    <div>\n" +
        "        third\n" +
        "    </div>\n" +
        "</div>";
Document doc = Jsoup.parse(html);
Elements select = doc.select("div > div");
System.out.println(select.get(1));

Output:

<div>
  second 
</div>

Another way could be using :eq(n) in CSS selector (from official Jsoup tutorial)

:eq(n): find elements whose sibling index is equal to n; e.g. form input:eq(1)

like

System.out.println(doc.select("div > div:eq(1)"));
like image 64
Pshemo Avatar answered Sep 16 '25 07:09

Pshemo