简体   繁体   中英

Get n-th child Element with 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.

There are few ways to to it. select returns Elements instance which extends ArrayList<Element> so you can select all child divs and pick one at specified index (starting from 0) like

 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>

You can also use :eq(n) selector which (from official tutorial )

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

like

System.out.println(doc.select("div > div:eq(1)"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM