以下java代码适用于带有类的html文件,例如css-sched-table-title。
但是我在html文件中有多个类名要找,例如css-sched-waypoints,css-sched-times。如何在jsoup中使用getElementsByClass方法组合搜索。我不想多次编写代码,因为我想保留订单。我的观点是我想要像
这样的东西doc.getElementsByClass(" css-sched-table-title" || doc.getElementsByClass(" css-sched-waypoints");
Document doc = Jsoup.parse(content);
Elements ele = doc.getElementsByClass("css-sched-table-title");
for (Element link : ele) {
String linkText = link.text();
System.out.println(linkText);
}
。
<tr ALIGN="CENTER">
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">6:15</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">6:20</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">6:24</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">6:34</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">6:34</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">6:40</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">6:46</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">6:54</td>
</tr>
<tr VALIGN="BOTTOM">
<TD> </TD>
<TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Townline and Southern</TD>
<TD> </TD>
<TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Clearbrook and Blueridge</TD>
<TD> </TD>
<TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Clearbrook and South Fraser</TD>
<TD> </TD>
<TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Ar. Bourquin Exchange</TD>
<TD> </TD>
<TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Lv. Bourquin Exchange</TD>
<TD> </TD>
<TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Downtown Abbotsford</TD>
<TD> </TD>
<TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">McMillan and Old Yale</TD>
<TD> </TD>
<TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Sandy Hill and Old Clayburn</TD>
</tr>
<tr ALIGN="CENTER">
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">8:12</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">8:17</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">8:21</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">8:31</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">8:34</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">8:40</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">8:46</td>
<td CLASS="css-sched-times"> </td>
<td CLASS="css-sched-times">8:54</td>
</tr>
答案 0 :(得分:1)
从我之前的查询中获取提示,当我尝试通过有效的td
语法组合3 Selector
时,我得到您期望的结果。
doc.select("td[class=css-sched-table-title], td[class=css-sched-waypoints], td[class=css-sched-times]")
注意,您可以在选择器语法中合并多个条件,例如Elements row = doc.select("td[class=css-sched-table-title], td[class=css-sched-waypoints], td[class=css-sched-times]");
,这有效地成为您的OR
运算符。
Elements row = doc.select("td[class=css-sched-table-title], td[class=css-sched-waypoints], td[class=css-sched-times]");
System.out.println("::Total Count::" + row.size());
Iterator<Element> iterator = row.listIterator();
while (iterator.hasNext()) {
Element element = iterator.next();
String id = element.attr("id");
String classes = element.attr("class");
String value = element.text();
System.out.println("Id : " + id + ", classes : " + classes
+ ", value : " + value);
}
给出,
::Total Count::25
Id : , classes : css-sched-table-title, value : Saturday - Afternoon
Id : , classes : css-sched-waypoints, value : Townline and Southern
Id : , classes : css-sched-waypoints, value : Clearbrook and Blueridge
Id : , classes : css-sched-waypoints, value : Clearbrook and South Fraser
Id : , classes : css-sched-waypoints, value : Ar. Bourquin Exchange
Id : , classes : css-sched-waypoints, value : Lv. Bourquin Exchange
Id : , classes : css-sched-waypoints, value : Downtown Abbotsford
Id : , classes : css-sched-waypoints, value : McMillan and Old Yale
Id : , classes : css-sched-waypoints, value : Sandy Hill and Old Clayburn
Id : , classes : css-sched-times, value :
Id : , classes : css-sched-times, value : 6:15
Id : , classes : css-sched-times, value :
Id : , classes : css-sched-times, value : 6:20
Id : , classes : css-sched-times, value :
Id : , classes : css-sched-times, value : 6:24
Id : , classes : css-sched-times, value :
Id : , classes : css-sched-times, value : 6:34
Id : , classes : css-sched-times, value :
Id : , classes : css-sched-times, value : 6:34
Id : , classes : css-sched-times, value :
Id : , classes : css-sched-times, value : 6:40
Id : , classes : css-sched-times, value :
Id : , classes : css-sched-times, value : 6:46
Id : , classes : css-sched-times, value :
Id : , classes : css-sched-times, value : 6:54
有关Selector
语法的详细用法,请参阅here.