Skip to content

Instantly share code, notes, and snippets.

@jaredrummler
Created November 30, 2016 01:47
Show Gist options
  • Select an option

  • Save jaredrummler/63ef35d002ef6e3da4195c3df923e8d3 to your computer and use it in GitHub Desktop.

Select an option

Save jaredrummler/63ef35d002ef6e3da4195c3df923e8d3 to your computer and use it in GitHub Desktop.
Get number of results for a Google Search

Using Jsoup:

public static int getGoogleResultsCount(String query) throws IOException {
  String url = "https://www.google.com/search?q=" + query;
  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();
  return Integer.parseInt(divResultStats.text().replaceAll("\\D", ""));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment