简体   繁体   中英

How to extract text with Jsoup from table

Maybe someone could help me with exracting information from html using jsoup?

Information needed is 23.90

<tr>
  <td class="leftcell" valign="top">
  <div onclick=
  "ShowHideTravelDetails('bookingPrice_TaxesToggleBox', 
'bookingPrice_TaxesToggleIcon', '/Images');" class="productheader">...</div>
</td>
<td class="rightcell emphasize" align="right" 
valign="bottom">$23.90</td></tr>

I can see it in few places in the html doc. I've tried using

Elements taxes = doc.select("td.rightcell.emphasize");

but it is not working.

Also tried extracting info as from table:

   Elements table = doc.select("table[class=selectiontable]");
        Elements rows = table.get(0).select("td[class^=rightcell emphasize]");
        for (Element row : rows) {


            Elements tds = row.select("td");
            System.out.println(tds.get(13));

Try like this I assume that you have a code like this . You need to do nested level iteration to get the result .

public class Test {
    public static void main(String[] args) {
        String html ="<table class=\"selectiontable\">\n" +
                "<tr>\n" +
                "  <td class=\"leftcell\" valign=\"top\">\n" +
                "  <div onclick=\n" +
                "  \"ShowHideTravelDetails('bookingPrice_TaxesToggleBox', \n" +
                "'bookingPrice_TaxesToggleIcon', '/Images');\" class=\"productheader\">...</div>\n" +
                "</td>\n" +
                "<td class=\"rightcell emphasize\" align=\"right\" \n" +
                "valign=\"bottom\">$23.90</td></tr>\n" +
                "</table>";


        Document document = Jsoup.parse(html);
        Elements elements = document.select(".selectiontable");
         for (Element element :elements){
             for (Element row : element.select("tr")) {
                 Elements tds = row.select("td");
                 if (tds.size() > 1) {
                     System.out.println(tds.get(1).text());
                 }
             }
         }
    }


}

output:

$23.90

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