Reputation: 2199
I am stuck at a place where I need to parse this website and display the Top PlayStation 3 Games by Metascore with their ratings. I am not able to come up with good parsing using JSoup as I just started developing using Jsoup.
I got the ratings and title like this. Any better ways ?
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());
}
Upvotes: 2
Views: 271
Reputation: 8617
The other way you can look at is look for a repetitive parent for both the tags (like div.main_stats
) you need and iterate over it gathering the elements:
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());
}
Output:
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
Upvotes: 2
Reputation: 2199
I came up with this code :
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());
}
Upvotes: 0