jSoup:是否无法正确解析两个具有相同值的连续 table 单元格?
jSoup: Am not being able to correctly parse two consecutive table cells with identical values?
我的Table(为了更好的可见性而缩短和格式化):
String htmlStr =
"<table class="xt">
<tbody>
<tr><th class="bg-warning" colspan="3">-</th></tr>
<tr><th class="bg-warning">2014-08-29</th><td>0</td><td>0.00</td><td>0.00</td></tr>
</tbody>
</table>"
我的代码:
org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
xt = doc.select("table.xt").first();
thCols = xt.select("tr").eq(1).select("th").size();
tdCols = xt.select("tr").eq(1).select("td").size();
结果
System.out.println(thCols); // prints 1
System.out.println(tdCols); // prints 2 - whereas I am expecting 3
为什么会这样?我进行了搜索 - 但我能找到的最好的是关于 jSoup 期望如何正确构造的评论 HTML5。但是,上面的 table 似乎是合规的,除非我严重遗漏了什么。还有什么原因?
这是最新的Jsoup 1.8.3中的一个bug,在Jsoup 1.8.2版本中引入。如果您切换回 1.8.1,它应该会按预期工作。另一种解决方案是为 tds 的数量创建一个选择器:
String htmlStr = ""
+"<table class=\"xt\">"
+" <tbody>"
+" <tr><th class=\"bg-warning\" colspan=\"3\">-</th></tr>"
+" <tr><th class=\"bg-warning\">2014-08-29</th><td>0</td><td>0.00</td><td>0.00</td></tr>"
+" </tbody>"
+"</table>";
Document doc = Jsoup.parse(htmlStr);
Element xt = doc.select("table.xt").first();
int thCols = xt.select("tr").eq(1).select("th").size();
int tdCols = xt.select("tr").eq(1).select("td").size();
int tdCols2 = xt.select("tr:eq(1)>td").size();
System.out.println(thCols); // prints 1
System.out.println(tdCols); // prints 2 - whereas I am expecting 3
System.out.println(tdCols2); // prints 3 as expected
这可能与所描述的错误相同 here
我的Table(为了更好的可见性而缩短和格式化):
String htmlStr =
"<table class="xt">
<tbody>
<tr><th class="bg-warning" colspan="3">-</th></tr>
<tr><th class="bg-warning">2014-08-29</th><td>0</td><td>0.00</td><td>0.00</td></tr>
</tbody>
</table>"
我的代码:
org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
xt = doc.select("table.xt").first();
thCols = xt.select("tr").eq(1).select("th").size();
tdCols = xt.select("tr").eq(1).select("td").size();
结果
System.out.println(thCols); // prints 1
System.out.println(tdCols); // prints 2 - whereas I am expecting 3
为什么会这样?我进行了搜索 - 但我能找到的最好的是关于 jSoup 期望如何正确构造的评论 HTML5。但是,上面的 table 似乎是合规的,除非我严重遗漏了什么。还有什么原因?
这是最新的Jsoup 1.8.3中的一个bug,在Jsoup 1.8.2版本中引入。如果您切换回 1.8.1,它应该会按预期工作。另一种解决方案是为 tds 的数量创建一个选择器:
String htmlStr = ""
+"<table class=\"xt\">"
+" <tbody>"
+" <tr><th class=\"bg-warning\" colspan=\"3\">-</th></tr>"
+" <tr><th class=\"bg-warning\">2014-08-29</th><td>0</td><td>0.00</td><td>0.00</td></tr>"
+" </tbody>"
+"</table>";
Document doc = Jsoup.parse(htmlStr);
Element xt = doc.select("table.xt").first();
int thCols = xt.select("tr").eq(1).select("th").size();
int tdCols = xt.select("tr").eq(1).select("td").size();
int tdCols2 = xt.select("tr:eq(1)>td").size();
System.out.println(thCols); // prints 1
System.out.println(tdCols); // prints 2 - whereas I am expecting 3
System.out.println(tdCols2); // prints 3 as expected
这可能与所描述的错误相同 here