我正在尝试从python应用程序运行谷歌搜索查询。有没有什么python接口可以让我这样做呢?如果没有,谁知道哪个Google API能让我做到这一点。谢谢。
发布于 2009-11-02 00:30:17
这里有一个简单的示例here (特别是缺少一些引号;-)。您将在web上看到的大多数内容都是旧的、已停止使用的SOAP API的Python接口--我所指的示例使用了更新的、受支持的AJAX API,这绝对是您想要的接口!
编辑:这是一个更完整的Python2.6示例,其中包含所有需要的引号&c;-)...:
#!/usr/bin/python
import json
import urllib
def showsome(searchfor):
query = urllib.urlencode({'q': searchfor})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
search_response = urllib.urlopen(url)
search_results = search_response.read()
results = json.loads(search_results)
data = results['responseData']
print 'Total results: %s' % data['cursor']['estimatedResultCount']
hits = data['results']
print 'Top %d hits:' % len(hits)
for h in hits: print ' ', h['url']
print 'For more results, see %s' % data['cursor']['moreResultsUrl']
showsome('ermanno olmi')发布于 2009-11-02 03:09:15
下面是移植到Python3上的亚历克斯的答案
#!/usr/bin/python3
import json
import urllib.request, urllib.parse
def showsome(searchfor):
query = urllib.parse.urlencode({'q': searchfor})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
search_response = urllib.request.urlopen(url)
search_results = search_response.read().decode("utf8")
results = json.loads(search_results)
data = results['responseData']
print('Total results: %s' % data['cursor']['estimatedResultCount'])
hits = data['results']
print('Top %d hits:' % len(hits))
for h in hits: print(' ', h['url'])
print('For more results, see %s' % data['cursor']['moreResultsUrl'])
showsome('ermanno olmi')发布于 2012-01-10 18:57:45
下面是我的方法:http://breakingcode.wordpress.com/2010/06/29/google-search-python/
下面是几个代码示例:
# Get the first 20 hits for: "Breaking Code" WordPress blog
from google import search
for url in search('"Breaking Code" WordPress blog', stop=20):
print(url)
# Get the first 20 hits for "Mariposa botnet" in Google Spain
from google import search
for url in search('Mariposa botnet', tld='es', lang='es', stop=20):
print(url)请注意,此代码不使用Google API,并且到目前为止(2012年1月)仍在运行。
https://stackoverflow.com/questions/1657570
复制相似问题