jsoup 使用总结3--高级用法之 not

jsoup 使用总结3--高级用法之 not

大部分时候,我们使用jsoup解析网页的时候,都是直接找到某一类元素,或者按某种selector查询;具体使用方法可以参考jsoup官网文档

例子代码:

String html = "<p>An <a href='http://example.com/'><b>example</b></a> link. this is other <a href="http://example.com/abc" style="color:red">linkB</a></p>";
Document doc = Jsoup.parse(html);
Elements links = doc.select("a");
for(Element e : links)
{
   String linkHref = link.attr("href"); 
   ...
}
Element link = doc.select("a").first();

如何我们想直接找到example的DOM元素呢,有没有其他方式
当然有了,下面是代码:

Element example = doc.select("a").not("[style]").first();

当然这里的例子比较简单,但是not的用法我是交给你了呀。

希望能解决你手边的问题。

另外推荐阅读jsoup的官网文档,我80%的问题都在官网找到了方法。

原文地址:https://www.cnblogs.com/jerrychen/p/4667309.html