谷歌搜索使用python脚本

sud*_*udh 32 python

谁能帮助我如何编写一个搜索谷歌的python脚本并打印出最佳结果的链接.

Man*_*ram 32

试试这个,它非常简单易用:https: //pypi.python.org/pypi/google

文档:https://breakingcode.wordpress.com/2010/06/29/google-search-python/

Github:https://github.com/MarioVilas/google

安装这个python包,用法就像这样简单:

# Get the first 5 hits for "google 1.9.1 python" in Google Pakistan
from google import search

for url in search('google 1.9.1 python', tld='com.pk', lang='es', stop=5):
    print(url)
Run Code Online (Sandbox Code Playgroud)


小智 26

也许,这样的事情?

import urllib
import json as m_json
query = raw_input ( 'Query: ' )
query = urllib.urlencode ( { 'q' : query } )
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
    title = result['title']
    url = result['url']   # was URL in the original and that threw a name error exception
    print ( title + '; ' + url )

阅读文档http://docs.python.org/

[编辑]由于AJAX API已经死了,你可以使用第三方服务,比如SerpApi,它们确实提供了一个Python库.

  • 这个api已不再可用.我们必须使用https://developers.google.com/custom-search/ (13认同)