【发布时间】:2015-06-03 01:07:15
【问题描述】:
我是 jsoup 的新手,可能用错了。我打算联系邮件列表,但 jsoup instructs to post here first。我正在尝试从表中选择<td> 元素,但返回的第一个元素实际上是<thead>。这是获取给定表的<td> 元素的错误方法吗?如果是这样,正确的方法是什么?
下面的简单代表性问题:
HTML:
<table id="results_table">
<thead>
<th>Header1</th>
</thead>
<tbody>
<tr>
<td>td1</td>
</tr>
</tbody>
</table>
测试:
String pageHtml = IOUtils.toString(this.getClass().getResourceAsStream("sample.html"));
Document doc = Jsoup.parse(pageHtml);
Element table = doc.getElementById("results_table");
Elements trs = table.getElementsByTag("tr");
System.out.println("Size: " + trs.size());
System.out.println("First Element: " + trs.get(0).html());
System.out.println("Second Element: " + trs.get(1).html());
收到的输出:
Size: 2
First Element: <th>Header1</th>
Second Element: <td>td1</td>
预期输出:
Size: 1
First Element: <td>td1</td>
//Index out of bounds exception
【问题讨论】: