我被困在需要解析的地方this网站并显示 Metascore 的热门 PlayStation 3 游戏及其评分。我无法使用 JSoup 进行良好的解析,因为我刚刚开始使用 Jsoup 进行开发。
我得到了这样的收视率和标题。还有更好的方法吗?
Document doc = Jsoup.connect(URL).userAgent("Mozilla").get();
// To get score
Elements links = doc.select("span.metascore_w.medium.game");
// To get title
Elements links = doc.select("h3.product_title");
for (Element link : links) {
System.out.println("text : " + link.text());
}
最佳答案
您可以查看的另一种方法是为您需要的两个标签(例如 div.main_stats
)查找重复的父级,并迭代它收集元素:
Elements parents = doc.select("div.main_stats");
for (Element child : parents) {
Element label = child.select("h3.product_title").first();
Element score = child.select("span.metascore_w.medium.game").first();
System.out.println("Game **" + label.text()+ "** has a Metascore of ->> " + score.text());
}
输出:
Game **XCOM: Enemy Within** has a Metascore of ->> 88
Game **Minecraft: PlayStation 3 Edition** has a Metascore of ->> 86
Game **Gran Turismo 6** has a Metascore of ->> 81
Game **Need for Speed: Rivals** has a Metascore of ->> 80
关于java - JSOUP网站HTML解析: Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21464752/