I'm trying to grab the value from the following HTML which constantly changes
<span class="rts-counter" rel="current_population">
<span class="rts-nr-sign"></span>
<span class="rts-nr-int rts-nr-10e9">7</span>
<span class="rts-nr-thsep">,</span>
<span class="rts-nr-int rts-nr-10e6">786</span>
<span class="rts-nr-thsep">,</span>
<span class="rts-nr-int rts-nr-10e3">396</span>
<span class="rts-nr-thsep">,</span>
<span class="rts-nr-int rts-nr-10e0">227</span>
</span>
My code so far doesn't produce anything
Document doc = Jsoup.connect("https://www.worldometers.info/world-population/").get();
Elements div = doc.select("div.maincounter-number");
Elements span = div.select("span");
System.out.println(span);
Output. The stuff I want is in the ...
<span class="rts-counter" rel="current_population">retrieving data... </span>
This is common for sites with dynamic content to use a placeholder as
<span class="rts-counter" rel="current_population">retrieving data... </span>
so this span can be filled with data later. Usually after executing another asynchronous request to server to load only the data that is changing.
Jsoup can't execute JavaScript but it's usually possible to make a direct call for additional data resources usually returned as JSON. But here it won't work and I have bad news...
The data you see is fake! I'm monitoring this site with Chrome's debugger and checking Network tab I see there's no communication with server and there's no data that's dynamically fetched. That means the data is retrieved only on first load and then JavaScript and some mathematical formula is used to increase the counters by a certain value after a certain time has passed giving it a bit of randomness to make it look convincing.
Initial data values are downloaded using this URL:
https://www.realtimestatistics.net/rts/init.php?callback=jsoncallback&host=worldometers&time=1590244162325&_=1590244161401 (link is dynamic and probaby expired by now)
but the result is somehow encoded. It seems to be decoded in this script https://www.realtimestatistics.net/rts/RTSp.js but it's minimized and hard to read.
So the get the original value you'd have to reverse engineer and analyze their RTSp.js to obtain initial value and the formula they're using to increment the counter.
Related info https://www.worldometers.info/licensing/what/