Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

python - perform a google search and return the number of results

The Google Web Search APIs appear to be dead (both the old SOAP and the newer AJAX). Is there a quick way to search Google for a string and return the number of results? I assume I just have to run the search and scrape the results, but I'd love to know if there's a better way.

Update: It turns out that any automated access to Google that doesn't use their new API https://developers.google.com/custom-search/json-api/v1/overview violates their terms of service, and is thus not recommended.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is still a free API, but here is a screen-scraper:

import requests
from bs4 import BeautifulSoup
import argparse

parser = argparse.ArgumentParser(description='Get Google Count.')
parser.add_argument('word', help='word to count')
args = parser.parse_args()

r = requests.get('http://www.google.com/search',
                 params={'q':'"'+args.word+'"',
                         "tbs":"li:1"}
                )

soup = BeautifulSoup(r.text)
print soup.find('div',{'id':'resultStats'}).text

Results:

$ python g.py jones
About 223,000,000 results
$ python g.py smith
About 325,000,000 results
$ python g.py 'smith and jones'
About 54,200,000 results
$ python g.py 'alias smith and jones'
About 181,000 results

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...