【发布时间】:2017-04-23 06:46:55
【问题描述】:
如何使用 Google Python 客户端库以编程方式使用 Google 自定义搜索 API 搜索引擎执行 advanced search,以便根据我查询的高级搜索的某些术语和参数返回第一个 n 链接列表? .
我试图检查documentation(我没有找到任何示例)和这个answer。但是,后者不起作用,因为目前不支持 AJAX API。到目前为止,我试过这个:
from googleapiclient.discovery import build
import pprint
my_cse_id = "test"
def google_search(search_term, api_key, cse_id, **kwargs):
service = build("customsearch", "v1",developerKey="<My developer key>")
res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
return res['items']
results = google_search('dogs', my_api_key, my_cse_id, num=10)
for result in results:
pprint.pprint(result)
还有这个:
import pprint
from googleapiclient.discovery import build
def main():
service = build("customsearch", "v1",developerKey="<My developer key>")
res = service.cse().list(q='dogs').execute()
pprint.pprint(res)
if __name__ == '__main__':
main()
因此,您知道如何使用 google 的搜索引擎 API 和 advanced search 吗?这就是我的凭据在 google 控制台中的外观:
【问题讨论】:
-
你得到什么错误?
-
@EugeneLisitsky,我没有收到任何错误。问题是我不明白如何使用谷歌的 API 制作 advanced search。例如,我如何以编程方式使用 google 查询所有在
english中包含the best dog food的urlsUK。
标签: python python-3.x google-api google-search-api google-api-python-client