我想使用jsoup
从仅某些特定属性和值的网页中提取元素。我已经完成了下面提到的方法,没有一个能很好地解决我的目的:
Jsoup的getElementsByAttributesMatching
此选择查询格式:
doc.select("table[width=100%]").select("table[cellpadding=0]").select("table[cellspacing=0]");
这也是:
doc.select("table[width=100%][cellpadding=0][cellspacing=0]");
当我使用这些方法时,我得到的元素具有我提到的属性以及其他属性。我想要的只是具有指定属性的元素。
有没有办法让这个蜷缩在一起?
答案 0 :(得分:0)
您的选择器已选择具有 3 属性的特定值的元素。所以元素至少有3个属性。但如果它具有完全 3属性,则那些是您指定的属性。
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