Consider an html document like this one
考虑这样的html文档
<div>
<p>...</p>
<p>...</p>
...
<p class="random_class_name">...</p>
...
</div>
How could we select all of the p
elements, but excluding the p
element with random_class_name
class?
如何选择所有的p元素,但不包括具有random_class_name类的p元素?
2 个解决方案
#1
8
Elements ps = body.select("p:not(.random_class_name)");
You can use the pseudo selector :not
您可以使用伪选择器:not
If the class name is not known, you still can use a similar expression:
如果不知道类名,仍然可以使用类似的表达式:
Elements ps = body.select("p:not([class])");
In the second example I use the attribute selector []
, in the first the normal syntax for classes.
在第二个示例中,我使用属性选择器[],在第一个示例中使用类的常规语法。
See the Jsoup docu about css selectors
请参阅有关css选择器的Jsoup文档
#2
1
Document doc = Jsoup.parse(htmlValue);
Elements pElements = doc.select("p");
for (Element element : pElements) {
String class = element.attr("class");
if(class == null){
//.....
}else{
//.....
}
}
#1
8
Elements ps = body.select("p:not(.random_class_name)");
You can use the pseudo selector :not
您可以使用伪选择器:not
If the class name is not known, you still can use a similar expression:
如果不知道类名,仍然可以使用类似的表达式:
Elements ps = body.select("p:not([class])");
In the second example I use the attribute selector []
, in the first the normal syntax for classes.
在第二个示例中,我使用属性选择器[],在第一个示例中使用类的常规语法。
See the Jsoup docu about css selectors
请参阅有关css选择器的Jsoup文档
#2
1
Document doc = Jsoup.parse(htmlValue);
Elements pElements = doc.select("p");
for (Element element : pElements) {
String class = element.attr("class");
if(class == null){
//.....
}else{
//.....
}
}