naotokui
11/3/2016 - 12:41 AM

Google search and get the number of search result

Google search and get the number of search result

import requests
from bs4 import BeautifulSoup
import argparse

def get_google_count(query):
    parser = argparse.ArgumentParser(description='Get Google Count.')
    parser.add_argument('word', help='word to count')
    r = requests.get('http://www.google.com/search',
                 params={'q':'"'+ query +'"',
                         "tbs":"li:1"}
                )
    soup = BeautifulSoup(r.text)
    result = soup.find('div',{'id':'resultStats'}).text
    count = int(''.join(result[2:-2].split(","))) # "約 22,400 件" -> 22400
    return count
    
#test
print get_google_count("query string")