X
X
XOM91K2021-10-29 23:39:06
Java
XOM91K, 2021-10-29 23:39:06

How to parse a web page using Jsoup?

public void methodAddDataTop3(TableView table) throws IOException {
        TableViewController tvController = new TableViewController();
        tvController.methodTableItemsClear(table);
        Document doc = Jsoup.connect("http://www.stoloto.ru/keno2/archive/").get();
        Elements newsHeadlines = doc.select("div.ashjf-0.dkbhVc");
            System.out.println(newsHeadlines.get(1).text());
    }

Hello!
How to parse this page? Other pages on this site are parsing fine, but this is the problem...
Ideally, you need to extract 20 digits of the circulation.
FxYpcgQ.png
The error is this, the error hints that I specified the wrong doc.select, if I specify on other pages, let's say the page with the top 3 lottery, Rapido, etc., then everything is parsed normally.
Caused by: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2021-10-30
@XOM91K

Good afternoon.
As noted by colleague Sergey Gornostaev , the content of this page is formed dynamically. Accordingly, jsoup will not work.
You need to make a GET request to the REST service URL and get json, which will then need to be parsed.
Open your browser console and go to the "Network" tab. Select XHR in the filters and examine the requests.
You will see this url:

https://www.stoloto.ru/p/api/mobile/api/v34/service/draws/archive?count=10&game=keno2

And in response, json is used with the draws branch.
Using either json2pojo online service, create a pojo and use (gson, DomParser or jackson) to parse this json.
jsoup is not needed here.
You can also parse the page using selenium (controlling the browser), but in my opinion this is a bad solution.

S
Sergey Gornostaev, 2021-10-29
@sergey-gornostaev

Start the debugger and see what data is being processed when the error occurs. Probably, the numbers you are interested in are formed by javascript, and therefore they are not on the page for JSoup.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question