简体   繁体   中英

Using jsoup with dynamic id's

I'm trying to use jsoup in order to parse a html file. It was done using tables to display products. Each product is inside one table whose id ranges from 1 to "n". Like the example bellow:

<table align="center" width="98%" id="A + 1">
   <tbody>
      <tr>
         <td valign="top" style="width: 03%;">
            <span class="line">1</span>
         </td>
         <td valign="top" style="width: 56%;">
            <span class="line">PRODNAME</span>
         </td>
         <td valign="top" style="width: 10%;">
            <span class="line">850.000</span>
         </td>
      </tr>
   </tbody>
</table>

So the first product will be on table with id "A + 1", the second one in "A + 2" and so on.

I'm not able to use selector to iterate over these tables. I'm doing:

Document doc = Jsoup.parse(html);
Elements products = doc.select("table[idˆ=A]");
for (Element product : products) {
   // do something
}

If I read it right ( http://jsoup.org/apidocs/org/jsoup/select/Selector.html ), doc.select("table[idˆ=A]") should retrieve all tables whose id attribute starts with an "A"...

But my Elements object (products) is empty... What I'm doing wrong?

I'm using jsoup 1.6.3, java 1.6.0_31 over a Mac OS X (10.7.4) with Netbeans 7.1.2.

Any help appreciated.

You seem to be using the wrong circumflex character in your selector, although I'm not sure if jsoup is supposed to return an empty result set or throw an exception on an invalid selector.

Anyway, try this instead:

Document doc = Jsoup.parse(html);
Elements products = doc.select("table[id^=A]");
for (Element product : products) {
   // do something
}

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