Jsoup-获取仅具有指定属性及其值的HTML标记 [英] Jsoup - getting a HTML tag with ONLY the specified attributes and its values
问题描述
我想使用jsoup
从仅具有 一些特定属性和值的页面中提取元素.我已经经历了以下提到的方法,但没有一个能很好地解决我的目的:
I want to use jsoup
to extract elements from a page that have only some specific attributes and values. I have gone through the below mentioned methods and none solved my purpose well:
-
Jsoup的
getElementsByAttributesMatching
选择查询的这种格式:
doc.select("table[width=100%]").select("table[cellpadding=0]").select("table[cellspacing=0]");
这也是:
This one too:
doc.select("table[width=100%][cellpadding=0][cellspacing=0]");
当我使用这些方法时,我得到的元素具有我提到的属性以及其他属性.我想要的是仅具有指定属性的元素.
When I use these methods I am getting elements which have the attributes I have mentioned plus other attributes too. What I want are the elements with ONLY the specified attributes.
有没有办法解决这个问题?
Is there a way to get pass this huddle?
推荐答案
您的选择器已经为那些 3 属性选择了具有特定值的元素.因此,该元素至少具有3个属性.但是,如果它完全具有 3个属性,则这些属性就是您指定的属性.
Your selector already select element having a specific value for those 3 attributes. So the element has at least 3 attributes. But if it has exactly 3 attributes, then those are the ones you specified.
for (Element el: doc.select("table[width=100%][cellpadding=0][cellspacing=0]"))
if (el.attributes().size() == 3)
// Do something
示例
Document doc = Jsoup.parse(
"<table width=100% cellpadding=0 cellspacing=0>OK</table>" +
"<table width=100% cellpadding=0 cellspacing=0 height=100%>NO</table>");
for (Element el: doc.select("table[width=100%][cellpadding=0][cellspacing=0]"))
if (el.attributes().size() == 3)
System.out.println(el.text());
输出
OK
这篇关于Jsoup-获取仅具有指定属性及其值的HTML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!