1

I tried to scrap the current word-population from this website, but there isnt any number in the span there. Im kinda new in pogramming so it would be helpful if someone could answer my question This is my code:

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

2 Answers2

0

in this link https://worldpopulationreview.com we can't find a class with name jsx-2221206670

Souhail HARRATI
  • 303
  • 2
  • 7
  • Hey I cahnged my code to this: ''' Document doc = Jsoup.connect("https://worldpopulationreview.com").userAgent("Mozilla").get(); 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); ''' but i only get as an output? – leschi4banane Jan 03 '22 at 21:56
  • I think because it is changed in real time, I have test with html code I find that it works well: Document doc = Jsoup.parse("

    Live Population: 7,916,081,627

    ");
    – Souhail HARRATI Jan 04 '22 at 21:03
0

To calculate the population, use the following solution:

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

you just have to do the mapping json to object (you can use Gson) and calculate the sum of the population per country

Souhail HARRATI
  • 303
  • 2
  • 7