简体   繁体   English

如何从表中使用Jsoup提取文本

[英]How to extract text with Jsoup from table

Maybe someone could help me with exracting information from html using jsoup? 也许有人可以帮助我使用jsoup从html中提取信息?

Information needed is 23.90 所需信息是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. 我可以在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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