Java 解析HTML网页
我使用JSoup解析来自此网站的数据:Java 解析HTML网页,java,html-parsing,jsoup,Java,Html Parsing,Jsoup,我使用JSoup解析来自此网站的数据: http://www.skore.com/en/soccer/england/premier-league/results/all/ 我得到了球队的名字和结果,我还需要得到得分手的名字(在结果下) 我正在尝试,但有麻烦,因为它不是在HTML 可能吗?如果是,如何获取?记分者信息是在AJAX请求后获取的(当您单击记分链接时发生)。您必须发出这样的请求并解析结果 对于Instance,以第一场比赛(曼联1x2曼城)为例,其标签为: <a data-y=
http://www.skore.com/en/soccer/england/premier-league/results/all/
我得到了球队的名字和结果,我还需要得到得分手的名字(在结果下)
我正在尝试,但有麻烦,因为它不是在HTML
可能吗?如果是,如何获取?记分者信息是在AJAX请求后获取的(当您单击记分链接时发生)。您必须发出这样的请求并解析结果
对于Instance,以第一场比赛(曼联1x2曼城)为例,其标签为:
<a data-y="r1-1229442" data-v="england-premierleague-manchesterunited-manchestercity-13april2013" style="cursor: pointer;">1 - 2</a>
例如:。然后解析结果
完整工作示例:
使用JSoup 1.7.1。如果使用maven,请将其添加到您的pom.xml
:
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.1</version>
</dependency>
org.jsoup
jsoup
1.7.1
您可以看到,记分员信息是在AJAX请求之后获得的(当您点击记分时会出现这种情况)。您必须发出这样的请求并解析结果。对于Instance,以第一场比赛(曼联1x2曼城)为例,它的标签是:。然后分析结果。你成功了吗?当我知道这是可能的时候,我正在努力我的意思是拯救得分手很容易。将此标记中的URL连接到每个匹配项将更加困难。它们有3个目标类:goal
、goalpenalty
和ownggoal
。代码现在可以正常工作了。哦,对我来说获取完整代码太容易了,但是谢谢。我看着它。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class ParseScore {
public static void main(String[] args) throws Exception {
Document doc = Jsoup.connect("http://www.skore.com/en/soccer/england/premier-league/results/all/").get();
System.out.println("title: " + doc.title());
Elements dls = doc.select("dl");
for (Element link : dls) {
String id = link.attr("id");
/* check if then it is a game <dl> */
if (id != null && id.length() > 3 && "rid".equals(id.substring(0, 3))) {
System.out.println("Game: " + link.text());
String idNoRID = id.replace("rid", "");
// String idNoRID = "1-1229442";
String scoreURL = "http://www.skore.com/en/scores/soccer/id/" + idNoRID + "?fmt=html";
Document docScore = Jsoup.connect(scoreURL).get();
Elements trs = docScore.select("tr");
for (Element tr : trs) {
Elements spanGoal = tr.select("span.goal");
/* only enter if there is a goal */
if (spanGoal.size() > 0) {
Elements score = tr.select("td.score");
String playerName = spanGoal.get(0).text();
String currentScore = score.get(0).text();
System.out.println("\t\tGOAL: " + currentScore + ": " + playerName);
}
Elements spanGoalPenalty = tr.select("span.goalpenalty");
/* only enter if there is a goal */
if (spanGoalPenalty.size() > 0) {
Elements score = tr.select("td.score");
String playerName = spanGoalPenalty.get(0).text();
String currentScore = score.get(0).text();
System.out.println("\t\tGOAL: " + currentScore + ": " + playerName + " (penalty)");
}
Elements spanGoalOwn = tr.select("span.goalown");
/* only enter if there is a goal */
if (spanGoalOwn.size() > 0) {
Elements score = tr.select("td.score");
String playerName = spanGoalOwn.get(0).text();
String currentScore = score.get(0).text();
System.out.println("\t\tGOAL: " + currentScore + ": " + playerName + " (own goal)");
}
}
}
}
}
}
title: Skore : Premier League, England - Soccer Results (All)
Game: F T Arsenal 3 - 1 Norwich
GOAL: 0 - 1: Michael Turner
GOAL: 1 - 1: Mikel Arteta (penalty)
GOAL: 2 - 1: Sébastien Bassong (own goal)
GOAL: 3 - 1: Lukas Podolski
Game: F T Aston Villa 1 - 1 Fulham
GOAL: 1 - 0: Charles N´Zogbia
GOAL: 1 - 1: Fabian Delph (own goal)
Game: F T Everton 2 - 0 Queens Park Rangers
GOAL: 1 - 0: Darron Gibson
GOAL: 2 - 0: Victor Anichebe
Game: F T Reading 0 - 0 Liverpool
Game: F T Southampton 1 - 1 West Ham
GOAL: 1 - 0: Gaston Ramirez
GOAL: 1 - 1: Andrew Carroll
Game: F T Manchester United 1 - 2 Manchester City
GOAL: 0 - 1: James Milner
...
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.1</version>
</dependency>