Java 在Jsoup中使用href解析表类时出现问题
我对JSOUP很陌生,只使用了几天,主要是从这个网站学习。现在我试图从下面的HTML中获取一些信息:Java 在Jsoup中使用href解析表类时出现问题,java,parsing,html-parsing,jsoup,Java,Parsing,Html Parsing,Jsoup,我对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/
<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>
Sun
17/05/15
我需要把主队,得分和客队从上面提取出来,但是我现在有这个问题。我需要链接和文本本身。以下是我的代码:
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();
}
试试看{
Document doc=Jsoup.connect(URL.get();
元素表=doc.select(“表[类=匹配]”).first();
元素行=表。选择(“tr”);
对于(int i=0;i只需用点分隔多个类:
td.team.team-a > a // first team
td.team.team-b > a // second team
td.score > a // score
似乎我错过了前面的“td”。我猜是新手错误。谢谢!抱歉重复评论,但上面的内容让我知道了团队本身的名称-我该如何获得链接本身?Axotic中的部分,你可以做一些类似doc.select(“td.team.team-a>a”).first().attr(“href”)的事情
@JonasCz这绝对让生活更轻松了,哈哈。非常感谢!