简体   繁体   中英

Jsoup - getting a HTML tag with ONLY the specified attributes and its values

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:

  1. Jsoup's getElementsByAttributesMatching

  2. This format of select query:

     doc.select("table[width=100%]").select("table[cellpadding=0]").select("table[cellspacing=0]"); 
  3. 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

Example

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM