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's getElementsByAttributesMatching
This format of select query:
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?
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());
Output
OK
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.