0

I'm working on code for parsing the weather site.

I found a CSS class with needed data on the web-site. How to pick up from there "on October 12" in the form of a string? (Tue, Oct 12)

public class Pars {
    private static Document getPage() throws IOException {
        String url = "https://www.gismeteo.by/weather-mogilev-4251/3-day/";
        Document page = Jsoup.parse(new URL(url), 3000);
        return page;
    }

    public static void main(String[] args) throws IOException {
        Document page = getPage();
        Element Nameday = page.select("div [class=date date-2]").first();
        String date = Nameday.select("div [class=date date-2").text();
        System.out.println(Nameday);
    }
}

The code is written for the purpose of parsing the weather site. On the page I found the right class in which only the date and day of the week I need. But at the stage of converting data from a class, an error crashes into a string.

Pavlo Stepura
  • 363
  • 2
  • 8
Sergant
  • 1
  • 1

1 Answers1

0

The problem is with class selector, it should look like this: div.date.date-2

Working code example:

public class Pars {

    private static Document getPage() throws IOException {
        String url = "https://www.gismeteo.by/weather-mogilev-4251/3-day/";
        return Jsoup.parse(new URL(url), 3000);
    }

    public static void main(String[] args) throws IOException {
        Document page = getPage();
        Element dateDiv = page.select("div.date.date-2").first();
        if(dateDiv != null) {
            String date = dateDiv.text();
            System.out.println(date);
        }
    }

}

Here is an answer to Your problem: Jsoup select div having multiple classes

In future, please make sure Your question is more detailed and well structured. Here is the "asking questions" guideline: https://stackoverflow.com/help/how-to-ask

Pavlo Stepura
  • 363
  • 2
  • 8
  • Thank you so much, after adding the condition, everything works as it should. But the class was left the same("div [class=date date-2]"), according to your version ("div.date.date-2") the result always returns null – Sergant Oct 11 '21 at 17:13