ashatte
ashatte

Reputation: 5538

Jsoup: Selecting elements that have a single class

I am parsing some tables from a website, and specifically I am trying to extract the following cells by class name:

<td class=" text_bold">example</td>

I use the standard Jsoup selector for extracting tags with a class, as follows:

Elements cells = doc.select("td.text_bold");

The problem is that there are other cells which are also selected because they have both the text_bold class and another class, for example:

<td class="text_bold text_align_left" valign="top" width="150">example</td>

Is there a simple way to filter only Elements that have a single class as specified in the select() method?

Upvotes: 0

Views: 1186

Answers (1)

Sage
Sage

Reputation: 15408

I had fallen in this situation before. However, the trick i have used is that:

  • first take all the element as Elements with target class: for your context "text_bold"
  • Then traverse each element comparing their class name which can be got using Element.className() function. If class name is "aClass bClass" format this function will return it as one class name.

For example:

Document doc = Jsoup.parse("<td class=\"text_bold text_align_left\" valign=\"top\" width=\"150\">example</td>
<td class=\" text_bold\">example</td>");
        Elements elms = doc.select("td.text_bold");
         for(Element e:elms)
             if(e.className().trim().equals("text_bold")) 
                             //^^^<--trim is required as, 
                            // their can be leading and trailing space 
             {
                 System.out.println(e.className());
                 // do my thing

             }

Upvotes: 2

Related Questions