[英]Getting element with no attributes using Jsoup
I have the following html, using Jsoup I'm trying to extract the text in the p section which does not have any attributes (the text "Some text 2" and not "Some text 1"). 我有以下html,使用Jsoup,我试图提取p部分中没有任何属性的文本(文本“ Some text 2”而不是“ Some text 1”)。
<div id="intro">
<h1 class="some class">
<p id="some_id">
Some text 1
</p>
<p>
Some text 2
</p>
</div>
I tried using the following Jsoup expression: 我尝试使用以下Jsoup表达式:
div[id=intro] > p:not(:has(@*))
But it doesn't work. 但这是行不通的。 Thanks for your help.
谢谢你的帮助。
I think you can use the JSOUP CSS selector p:not([^])
, which would select any p that does not match having an attribute starting with anything. 我认为您可以使用JSOUP CSS选择器
p:not([^])
,该选择器将选择任何不匹配且具有以任何内容开头的属性的p。
String html = "<div id=\"intro\">"
+ "<h1 class=\"some class\">"
+ "<p id=\"some_id\">"
+ "Some text 1"
+ "</p>"
+ "<p name=\"some_name\">"
+ "Some text A"
+ "</p>"
+ "<p data>"
+ "Some text B"
+ "</p>"
+"<p>"
+ "Some text 2"
+"</p>"
+"</div> ";
Document doc = Jsoup.parse(html);
Elements els = doc.select("p:not([^])");
for (Element el:els){
System.out.println(el.text());
}
the above example will only print out 上面的例子只会打印出来
Some text 2
because only this p element has no attributes. 因为只有这个p元素没有属性。
Note that the selector p[^]
will pick all p elements that do have an attribute. 请注意,选择器
p[^]
将选择所有具有属性的p个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.