1

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 is the 10,200,000 articles

How can I use Jsoup and any of its components to grab that number?

I tried:
Document document = Jsoup.connect(url).get(); Elements description = document.select("div#resultStats"); desc = description.attr("content");

Note: I am using Android Studio and I want to save the result into a matrix.

Edit: Here is what I see for the number of articles on the HTML source code.

sampjr
  • 103
  • 1
  • 2
  • 14
  • The downloaded document doesn't have resultStats (at least in testing at http://try.jsoup.org). Try using this: http://stackoverflow.com/a/11569731/1048340 – Jared Rummler Oct 28 '15 at 21:02
  • Try using desc = description.text() – vab Oct 29 '15 at 09:08
  • @Jared, Google search API does not exist anymore AFAIK, so that will not work. – Jonas Czech Oct 31 '15 at 19:41
  • It's deprecated but still works. https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=jeb%20bush%20barack%20obama Looks like the "resultStats" are different though. :\ – Jared Rummler Oct 31 '15 at 19:48
  • @JaredRummler It doesn't because the document contains some Javascript in charge of building the final HTML code. – Stephan Jan 15 '16 at 10:23

1 Answers1

5

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/

Stephan
  • 41,764
  • 65
  • 238
  • 329