K
K
knagornykh2021-02-08 18:31:35
Java
knagornykh, 2021-02-08 18:31:35

Why sometimes I get data and sometimes not, although I don’t change the code?

I want to get weather data from this page: https://www.google.com/search?client=opera&q=weather...

public class Parser2 {

    public static void main(String[] args) throws IOException {
        Parser2 parser = new Parser2();
        System.out.println(parser.valueList());
        System.out.println(parser.allPrintConsole());
    }

    static Elements getPage() throws IOException // возвращает всю таблицу с погодой
    {
        String url = "https://www.google.com/search?client=opera&q=погода+довск&sourceid=opera&ie=UTF-8&oe=UTF-8";
        Document page = Jsoup.connect(url).get();
        Elements tableWth =page.select("div[class=nawv0d vk_c]"); // вся таблица с погодой

        return tableWth;
    }

    public ArrayList<String> allPrintConsole() throws IOException
    {
        Elements tableWth = getPage();

        String weekData = tableWth.select("div[class=wob_dts vk_gy vk_sh]").text(); // ячейка дата с временем
        String temp = tableWth.select("span[class=wob_t TVtOme]").text(); //кол-во градусов, можно будет взять для: от до
        String whater = tableWth.select("span[id=wob_hm]").text();
        String weter = tableWth.select("span[id=wob_ws]").text();

        ArrayList<String> print = new ArrayList<>(); //тут храняться данные
        print.add(weekData);
        print.add(temp+"°С");
        print.add(whater);
        print.add(weter);

        return print;

    }

    public static   ArrayList<String> weekList() throws IOException {

        Elements peek = getPage().select("div[class=wob_dfc]");

        String[] weekName = peek.select("div[class=QrNVmd Z1VzSb]").text().split(" "); // дни недели

        ArrayList<String> week = new ArrayList<>();

        for (int i = 0; i < weekName.length; i++) {
            week.add(weekName[i]); // week.size() = 8
        }
        return week;
    }

    public ArrayList<String> valueList() throws IOException {

        Elements peek = getPage().select("div[class=wob_dfc]");

        ArrayList<String> list = new ArrayList<>();

        for (int i = 0; i < 32; i+=2) {
            String first = peek.select("span[class=wob_t]").get(i).text();
            list.add(first); //list.size() = 16
        }
        return list;
    }
}


Sometimes the data is displayed, and sometimes not, the code is not changed
6021591bda01d377016263.png
602159275d9dc931577076.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-02-08
Hasanly @azerphoenix

Good afternoon!
The first thing I will say is that it is not a good idea to use a parser (Jsoup or whatever) to get the weather forecast.
Use the API services provided for this. For example, OpenWeatherMap
Next is the answer to your question:

Why sometimes I get data and sometimes not, although I don’t change the code?

You may not change the code you wrote, but the page (DOM) of the site itself may change due to which you are seeing an error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question