简体   繁体   中英

Cannot parse webpage of google with Jsoup

I'm making simple weather parser on Java via jsoup. Yesterday it worked well but today I instanly got ERROR 429 (too many requests if i'm not wrong). Here's my code, how can i fix it?

public class Parser {
    public static void parse() throws IOException {
        Scanner scanner = new Scanner(System.in);
        
        String city, temp, hum, wind, status, name, day;
        city = scanner.nextLine();

        Document doc = Jsoup.connect("https://www.google.com/search?q="+city+" weather").timeout(10*1000).get();
        Element tempElem = doc.selectFirst("span.wob_t.q8U8x");

        temp = Objects.requireNonNull(doc.selectFirst("span.wob_t.q8U8x")).text();
        hum = Objects.requireNonNull(doc.selectFirst("#wob_hm")).text();
        wind = Objects.requireNonNull(doc.selectFirst("#wob_ws")).text();
        status = Objects.requireNonNull(doc.selectFirst("#wob_dc")).text();
        name = Objects.requireNonNull(doc.selectFirst("#wob_loc.q8U8x")).text();
        day = Objects.requireNonNull(doc.selectFirst("#wob_dts")).text();
        
        if(tempElem == null){
            System.out.println("City's not found");
            System.exit(0);
        }

        System.out.println("Weather in " + name + ". ("+day+", "+status+")"+
                "\n Temperature: " + temp + "°C" +
                "\n Humanity: " + hum +
                "\n Wind speed:  " + wind);
    }
}

I've tried adding.timeoute() to Jsoup.connect() but I see no results there.

you have probably exceeded the daily search request limit of google. If too many requests are sent from the same IP, it will be blocked. A solution would be to use a weather api that allows more requests per day.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM