我是JSOUP的新手,使用它才几天,主要是从这个网站上学习的。现在,我正试图从以下HTML中获取一些信息:
<td class="day no-repetition">Sun</td>
<td class="full-date" nowrap="nowrap">17/05/15</td>
<td class="competition"><a href="/national/england/premier-league/20142015/regular-season/r25191/" title="Premier League">PRL</a></td>
<td class="team team-a ">
<a href="/teams/england/sunderland-association-football-club/683/" title="Sunderland">
Sunderland
</a>
</td>
<td class="score-time score">
<a href="/matches/2015/05/16/england/premier-league/sunderland-association-football-club/leicester-city-fc/1704225/" class="result-draw">
0 - 0
</a>
</td>
<td class="team team-b ">
<a href="/teams/england/leicester-city-fc/682/" title="Leicester City">
Leicester City
</a>
</td>
<td class="events-button button first-occur">
</td>
<td class="info-button button">
<a href="/matches/2015/05/16/england/premier-league/sunderland-association-football-club/leicester-city-fc/1704225/" title="More info">More info</a>
</td>
我需要从上面提取主队、得分和客场,但我目前对此有问题。我需要链接和文本本身。下面是我的代码:
try {
Document doc = Jsoup.connect(URL).get();
Element table = doc.select("table[class=matches]").first();
Elements rows = table.select("tr");
for (int i=0; i<rows.size(); i++){
Element row = rows.get(i);
Elements data = row.select("td[class=team.team-a]");
System.out.println(data.text());
}
} catch (IOException e) {
e.printStackTrace();
}
到目前为止,这还没有奏效。我尝试了"team.team.a"、"team.team.a"和它的所有其他变体。我设法获得了"competition"类中的数据,当我将("td[class=team.team=a]")替换为(td[class=competition])时,这是有效的,但这对任何有链接的类都不起作用。
非常感谢您的协助!
只需用一个点分隔多个类:
td.team.team-a > a // first team
td.team.team-b > a // second team
td.score > a // score