使用 JSoup returns 无提取 table 行数据

Extract table row data using JSoup returns nothing

我正在尝试使用 JSoup library.But 提取 table 行数据 library.But 它没有给我输出结果。

Here is my code so far :

String html = "<tbody>"
                + "<tr>"
                + "<td><strong>Fit</strong></td>"
                + "<td>Regular</td>"
                + "</tr>"
                + "<tr>"
                + "<td><strong>Color</strong></td>"
                + "<td>Multi</td>"
                + "</tr>"
                + "<tr>"
                + "<td><strong>Style</strong></td>"
                + "<td>Checked</td>"
                + "</tr>"
                + "<tr>"
                + "<td><strong>Fabric</strong></td>"
                + "<td>Cotton</td>"
                + "</tr>"
                + "<tr>"
                + "<td><strong>Model Stats</strong></td>"
                + "<td> This model has height 5'9\",Bust 32\",Waist 28\",Hip 36\"and is Wearing Size 10.</td>"
                + "</tr>"
                + "</tbody>";

        Document doc = Jsoup.parse(html);


        for (Element table : doc.select("tbody")) {
            for (Element row : table.select("tr")) {
                Elements tds = row.select("td");
                for (Element td : tds) {
                    System.out.println(td.text());
                }
            }
        }

如果有人能建议我一种如下所示的输出方式,我将不胜感激:

<strong>Fit</strong>
Regular
<strong>Color</strong>
Multi
<strong>Style</strong>
Checked
<strong>Fabric</strong>
Cotton ... etc..

谢谢。

问题是你的html。您应该在 <table></table> 处添加 <table>end 你的 html 变量,否则 Jsoup 将无法正确解析你的 html,导致你的 <tbody> 转换为 <body>,这就是为什么您无法在查询中 select 它。

此外,要生成所需的输出,请使用 td.html() 而不是 td.text()