How to get #resultStats from Google using Jsoup

后端 未结 1 772
予麋鹿
予麋鹿 2021-01-25 02:48

I am trying to get the number of articles that Google shows us:

This is a Google search of jeb bush barack obama, and it shows the number that I need, which

相关标签:
1条回答
  • 2021-01-25 03:21

    Actually, you may be getting some optimized javascript code (for modern browsers) that need to be run to see actual results stats. Instead, change your user agent string (for a oldest browser UA string) and the url like in the code below:

    DEMO

    http://try.jsoup.org/~iYErM3BgfjILVJZshDMkAd-XQCk

    SAMPLE CODE

    String url = "https://www.google.com/search?q=jeb+bush+barack+obama";
    
    Document document = Jsoup //
                       .connect(url) //
                       .userAgent("Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)") //
                       .get();
    
    Element divResultStats = document.select("div#resultStats").first();
    if (divResultStats==null) {
        throw new RuntimeException("Unable to find results stats.");
    }
    
    System.out.println(divResultStats.text());
    

    OUTPUT (as of this writing...)

    About 10,500,000 results
    

    Tested on Jsoup 1.8.3

    More UA Strings: http://www.useragentstring.com/

    0 讨论(0)
提交回复
热议问题