简体   繁体   中英

How can I select a div that have no id or an attribute with Jsoup?

I need to select a div using Jsoup. I can select divs using their id or class by getElementById() for ID and getElementsByClass() for class attribute. However, the div i need to select is like below...

<div><h2 class='title'>Example</h2> 
.....
......
...... </div>

I must select this div. Div's unique property is just "Example" value in the < h2 > tag. So i must select the div according to < h2 > tag's text value. What should I do for this? please help Thanks...

Try using the following selector:

Elements e = doc.select("div:has(h2)");

This will select any div which contains a h2 tag. You can squeeze your selection set one step further by using the following:

Elements e = doc.select("div:has(h2:contains(Example))");

This will select all div which contains a h2 tag which also contains the text Example (case insensitive)

You can check out all the ways to combine selector syntax from http://jsoup.org/cookbook/extracting-data/selector-syntax

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