本文介绍了如何创建具有重复数据的Jsoup.select.Elements?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页:

<table id="timetable" class="table gradient-table">
<tbody>
    <tr>
      <td  class="time">
      <div>10:30 12:05</div>
      </td>
      <td  class="time">
      <div>12:30 14:05</div>
      </td>
      <td  class="time">
      <div>12:30 14:05</div>
      </td>
      <td  class="time">
      <div>14:30 16:05</div>
      </td>
      <td  class="time">
      <div>16:30 18:05</div>
      </td>
    </tr>
</tbody>
</table>

我在做:

doc.select("table[id=timetable]").select("tbody").select("td[class=time]");

我应该得到:

10:30 12:05
12:30 14:05
12:30 14:05
14:30 16:05
16:30 18:05
but I got:
10:30 12:05
12:30 14:05
14:30 16:05
16:30 18:05

我做对了吗?

推荐答案

似乎您遇到了Jsoup中的一个已知问题(请参阅 luksch's答案以获取详细信息).但是,这是在Jsoup 1.8.3上工作的解决方法.

It seems you have hit a known issue from Jsoup (see luksch's answer for details). However, here is a workaround working on Jsoup 1.8.3.

table#timetable > tbody > tr > td.time

示例代码

String html = "<table id=\"timetable\" class=\"table gradient-table\">\n<tbody>\n    <tr>\n      <td  class=\"time\">\n      <div>10:30 12:05</div>\n      </td>\n      <td  class=\"time\">\n      <div>12:30 14:05</div>\n      </td>\n      <td  class=\"time\">\n      <div>12:30 14:05</div>\n      </td>\n      <td  class=\"time\">\n      <div>14:30 16:05</div>\n      </td>\n      <td  class=\"time\">\n      <div>16:30 18:05</div>\n      </td>\n    </tr>\n</tbody>\n</table>";

Document doc = Jsoup.parse(html);

for (Element elt : doc.select("table#timetable > tbody > tr > td.time")) {
    System.out.println(elt.text());
}

输出

10:30 12:05
12:30 14:05
12:30 14:05
14:30 16:05
16:30 18:05

在Jsoup 1.8.3上进行了测试

这篇关于如何创建具有重复数据的Jsoup.select.Elements?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:19