JSoup 解析器未拾取 table 元素

JSoup parser not picking up table element

JSoup 这里。我有以下 HTML:

<html><head>
<title>My Soup Materials</title>
<!--mstheme--><link rel="stylesheet" type="text/css" href="../../_themes/ice/ice1011.css"><meta name="Microsoft Theme" content="ice 1011, default">
</head>
<body><center><table width="92%"><tbody>
<tr>
<td><h2>My Soup Materials</h2>

<table width="100%%" cellspacing="0" cellpadding="0">

<tbody>
<tr>
<td align="left"><b>Origin:</b> Belgium</td>
<td align="left"><b>Count:</b> 2 foos</td>
</tr>

<tr>
<td align="left"><b>Supplier:</b> </td>
<td align="left"><b>Must Burninate:</b> Yes</td>
</tr>

<tr>
<td align="left"><b>Type:</b> Fizzbuzz</td>
<td align="left"><b>Add Afterwards:</b> No</td>
</tr>

</tbody>
</table>
<br>
<b><u>Notes</b></u><br>Drink more ovaltine</td>
</tr>

</tbody>
</table>
</center></body>
</html>

当我运行这段代码时:

String htmlString = "<html><head><title>My Soup Materials</title><!--mstheme--><link rel=\"stylesheet\" type=\"text/css\" href=\"../../_themes/ice/ice1011.css\"><meta name=\"Microsoft Theme\" content=\"ice 1011, default\"></head><body><center><table width=\"92%\"><tbody><tr><td><h2>My Soup Materials</h2><table width=\"100%%\" cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td align=\"left\"><b>Origin:</b> Belgium</td><td align=\"left\"><b>Count:</b> 2 foos</td></tr><tr><td align=\"left\"><b>Supplier:</b> </td><td align=\"left\"><b>Must Burninate:</b> Yes</td></tr><tr><td align=\"left\"><b>Type:</b> Fizzbuzz</td><td align=\"left\"><b>Add Afterwards:</b> No</td></tr></tbody></table><br><b><u>Notes</b></u><br>Drink more ovaltine</td></tr></tbody></table></center></body></html>";
Document document = Jsoup.parse(htmlString);
Elements allTables = document.select("table");
Element table = allTables.get(0);

allTables 元素的大小为 0 且为空(没有 children、没有属性等)。因此,当我去获取第一个 table 时,我得到了一个 IndexOutOfBoundsException为什么? 我本以为会有很多 children,从“<h2>My Soup Materials</h2>”开始,等等

第一个

你的 html 有两个 table

第二

html table 有部分:header、body、行、列等。您需要创建更详细的算法才能获得 children

Element table = doc.select("table").get(0); //select the first table.
Elements rows = table.select("tr");

//first row is the col names or header, so skip it.
for (int i = 1; i < rows.size(); i++) { 
    Element row = rows.get(i);//get entire row
    Elements cols = row.select("td");//get cols of this row
    //get specific row
    System.out.printl(cols.get(5).text());
}

这可能归结为您的配置或您的 Java 代码中与您的问题不同的地方。我 运行 从问题中 Java 确切的 4 行代码并检查 allTables.size() 返回 2.

使用 Java 17 和当前最新版本的 JSoup (1.14.3)

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class App {
    public static void main(String[] args) {
        String htmlString = "<html><head><title>My Soup Materials</title><!--mstheme--><link rel=\"stylesheet\" type=\"text/css\" href=\"../../_themes/ice/ice1011.css\"><meta name=\"Microsoft Theme\" content=\"ice 1011, default\"></head><body><center><table width=\"92%\"><tbody><tr><td><h2>My Soup Materials</h2><table width=\"100%%\" cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td align=\"left\"><b>Origin:</b> Belgium</td><td align=\"left\"><b>Count:</b> 2 foos</td></tr><tr><td align=\"left\"><b>Supplier:</b> </td><td align=\"left\"><b>Must Burninate:</b> Yes</td></tr><tr><td align=\"left\"><b>Type:</b> Fizzbuzz</td><td align=\"left\"><b>Add Afterwards:</b> No</td></tr></tbody></table><br><b><u>Notes</b></u><br>Drink more ovaltine</td></tr></tbody></table></center></body></html>";
        Document document = Jsoup.parse(htmlString);
        Elements allTables = document.select("table");
        Element table = allTables.get(0);

        System.out.println(allTables.size()); // prints 2
    }
}