如何使用重复数据创建Jsoup.select.Elements?

我有一个网页:

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

我在做:

 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的回答 )。 但是,这是一个解决Jsoup 1.8.3的解决方法。

 table#timetable > tbody > tr > td.time 

示例代码

 String html = "\n\n \n \n \n \n \n \n \n\n
\n
10:30 12:05
\n
\n
12:30 14:05
\n
\n
12:30 14:05
\n
\n
14:30 16:05
\n
\n
16:30 18:05
\n
"; Document doc = Jsoup.parse(html); for (Element elt : doc.select("table#timetable > tbody > tr > td.time")) { System.out.println(elt.text()); }

OUTPUT

 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 1.8.2和JSoup 1.8.3中遇到了一个已知错误。 请参阅问题#614和#664

为避免这种情况,您应尽可能降级到Jsoup版本1.8.1,或确保不使用Elements(复数!)类的select方法。 单个元素或整个解决方案上的CSS选择器似乎不受影响,因此您也可以使用@Stephan的解决方案。