【问题标题】:No number showing when trying to scrape an number with Jsoup尝试使用 Jsoup 抓取数字时没有显示数字
【发布时间】:2022-01-26 23:01:42
【问题描述】:

我试图从这个网站上删除当前的词群,但那里的跨度中没有任何数字。 我在编程方面有点新,所以如果有人能回答我的问题会很有帮助 这是我的代码:

package org.jsoup.examples;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;


public class Main {
    public static void main(String[] args) {



        
    try {

        Document doc = Jsoup.connect("https://worldpopulationreview.com").userAgent("Mozilla").get();
        Elements ele = doc.getElementsByClass("jsx-2221206670");
        //System.out.println(ele);
        
        String htmlString = ele.toString();
        Document doc2 = Jsoup.parse(htmlString);
        Elements ele2 = doc2.getElementsByTag("span");
        System.out.println(ele2);

        
        

        }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    }
}

【问题讨论】:

    标签: web-scraping jsoup


    【解决方案1】:

    在此链接https://worldpopulationreview.com 中我们找不到名为 jsx-2221206670 的类

    【讨论】:

    • 嘿,我把我的代码改成了:''' Document doc = Jsoup.connect("worldpopulationreview.com").userAgent("Mozilla")…; Elements ele = doc.getElementsByClass("styled-mini"); //System.out .println(ele); String htmlString = ele.toString(); Document doc2 = Jsoup.parse(htmlString); Elements ele2 = doc2.getElementsByTag("span"); System.out.println(ele2); ''' 但是我只得到 作为输出?
    • 我认为因为它是实时更改的,所以我用 html 代码进行了测试,我发现它运行良好: Document doc = Jsoup.parse("

      活人口:7,916,081,627

      ");
    【解决方案2】:

    要计算人口,请使用以下解决方案:

    Element scriptTag = doc.getElementById("__NEXT_DATA__");
     
           
    System.out.println(scriptTag.data());
    
    

    您只需将 json 映射到对象(您可以使用 Gson)并计算每个国家/地区的人口总和

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多