简体   繁体   English

如何使用Jsoup选择没有ID或属性的div?

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

I need to select a div using Jsoup. 我需要使用Jsoup选择一个div。 I can select divs using their id or class by getElementById() for ID and getElementsByClass() for class attribute. 我可以使用其ID或类选择div,通过ID的getElementById()和class属性的getElementsByClass()来选择div。 However, the div i need to select is like below... 但是,我需要选择的div如下所示...

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

I must select this div. 我必须选择这个div。 Div's unique property is just "Example" value in the < h2 > tag. Div的唯一属性只是<h2>标记中的“ Example”值。 So i must select the div according to < h2 > tag's text value. 因此,我必须根据<h2>标签的文本值选择div。 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. 这将选择任何包含h2标签的div。 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) 这将选择所有包含h2标签的div,其中也包含文本Example(不区分大小写)

You can check out all the ways to combine selector syntax from http://jsoup.org/cookbook/extracting-data/selector-syntax 您可以从http://jsoup.org/cookbook/extracting-data/selector-syntax中查看所有组合选择器语法的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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