Skip to content

Instantly share code, notes, and snippets.

@erikbern
Created March 13, 2017 02:34
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save erikbern/7e10efd98e93af94445e250c6d8d2bd0 to your computer and use it in GitHub Desktop.
Save erikbern/7e10efd98e93af94445e250c6d8d2bd0 to your computer and use it in GitHub Desktop.
Get number of search results from Google
def get_n_results_dumb(q):
r = requests.get('http://www.google.com/search',
params={'q': q,
"tbs": "li:1"})
r.raise_for_status()
soup = bs4.BeautifulSoup(r.text)
s = soup.find('div', {'id': 'resultStats'}).text
if not s:
return 0
m = re.search(r'([0-9,]+)', s)
return int(m.groups()[0].replace(',', ''))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment