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.
Advertisement
Answer
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" + " firstn" + " </div>n" + " <div>n" + " secondn" + " </div>n" + " <div>n" + " thirdn" + " </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; e.g. form input:eq(1)
like
System.out.println(doc.select("div > div:eq(1)"));