本文介绍了使用Jsoup选择没有类的HTML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑这样的html文档
Consider an html document like this one
<div>
<p>...</p>
<p>...</p>
...
<p class="random_class_name">...</p>
...
</div>
我们如何选择所有p
元素,但不包括random_class_name
类的p
元素?
How could we select all of the p
elements, but excluding the p
element with random_class_name
class?
推荐答案
Elements ps = body.select("p:not(.random_class_name)");
您可以使用伪选择器: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.
这篇关于使用Jsoup选择没有类的HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!