【问题标题】:JSOUP Website HTML Parsing : JavaJSOUP 网站 HTML 解析:Java
【发布时间】:2014-01-30 18:33:45
【问题描述】:

我被困在一个地方,我需要解析 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());
      }

【问题讨论】:

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

标签: java web-scraping jsoup


【解决方案1】:

您可以查看的另一种方法是为您需要的两个标签(如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

【讨论】:

    【解决方案2】:

    我想出了这个代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 2012-02-22
      • 2015-09-30
      相关资源
      最近更新 更多