2014-01-30 121 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