2014-01-30 85 views
2

我被困在那里我需要解析this网站,并通过与他们的收视率Metascore显示前的PlayStation 3游戏的地方。我刚开始使用Jsoup开发时,我无法使用JSoup进行良好的解析。JSOUP网站HTML解析:Java的

我得到了这样的评级和标题。有更好的方法吗?

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()); 
     } 
+1

您可以发布您的尝试以获得更快的解决方案 – PopoFibo

+0

@PopoFibo代码添加 – AnujKu

回答

0

我想出了这个代码:

Element div = doc.select("ol.list_products.list_product_summaries").first(); 
     for (Element element : div.children()) { 
     System.out.println(element.select("span.metascore_w.medium.game").text()); 
     System.out.println(element.select("h3.product_title").text()); 
     } 
2

你可以看看其他的方式,在的就是寻找一个重复的父母为这两个标签(比如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