如何使用google customsearch API查询高级搜索?

问题描述 投票:0回答:4

如何以编程方式使用 Google Python 客户端库通过 Google 自定义搜索 API 搜索引擎进行高级搜索,以便根据我查询的高级搜索的某些术语和参数返回第一个

n
链接的列表?

我尝试检查文档(我没有找到任何示例),以及这个答案。然而,后者不起作用,因为目前不支持 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()

因此,您知道如何使用谷歌的搜索引擎 API 进行高级搜索吗?这就是我的凭据在谷歌控制台中的样子:

credentials

python python-3.x google-api google-search-api google-api-python-client
4个回答
9
投票
首先,您需要按照

此处所述定义自定义搜索,然后确保您的 my_cse_id

 与 google API 
自定义搜索 (cs) id 匹配,例如

cx='017576662512468239146:omuauf_lfve'

是一个搜索引擎,仅搜索以

.com

结尾的域名。

接下来我们需要我们的

developerKey

from googleapiclient.discovery import build service = build("customsearch", "v1", developerKey=dev_key)

现在我们可以执行搜索了。

res = service.cse().list(q=search_term, cx=my_cse_id).execute()

我们可以使用

此处描述的参数添加其他搜索参数,例如语言或国家/地区,例如

res = service.cse().list(q="the best dog food", cx=my_cse_id, cr="countryUK", lr="lang_en").execute()

会用英语搜索“最好的狗粮”,该网站必须来自英国。


以下修改后的代码对我有用。

api_key

 由于从未使用过而被删除。

from googleapiclient.discovery import build my_cse_id = "012156694711735292392:rl7x1k3j0vy" dev_key = "<Your developer key>" def google_search(search_term, cse_id, **kwargs): service = build("customsearch", "v1", developerKey=dev_key) res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute() return res['items'] results = google_search('boxer dogs', my_cse_id, num=10, cr="countryCA", lr="lang_en") for result in results: print(result.get('link'))

输出

http://www.aboxerworld.com/whiteboxerfaqs.htm http://boxerrescueontario.com/?section=available_dogs http://www.aboxerworld.com/abouttheboxerbreed.htm http://m.huffpost.com/ca/entry/10992754 http://rawboxers.com/aboutraw.shtml http://www.tanoakboxers.com/ http://www.mondlichtboxers.com/ http://www.tanoakboxers.com/puppies/ http://www.landosboxers.com/dogs/puppies/puppies.htm http://www.boxerrescuequebec.com/

    

2
投票
如果您不想使用 google discovery api,可以使用 python requests 库作为替代方案:

import requests, pprint q='italy' api_key='AIzaSyCs.....................' q = requests.get('https://content.googleapis.com/customsearch/v1', params={ 'cx': '013027958806940070381:dazyknr8pvm', 'q': q, 'key': api_key} ) pprint.pprint(q.json())
    

1
投票
这已经晚了,但希望它对某人有帮助......

用于高级搜索用途

response=service.cse().list(q="mysearchterm", cx="017576662512468239146:omuauf_lfve", ).execute()

list()

方法接受更多参数来帮助推进您的搜索...在此处检查参数:
https://developers.google.com/custom-search/json-api/v1/reference/cse/list


0
投票
这里是完整的文档:

https://developers.google.com/custom-search/v1/reference/rest/v1/cse/list

© www.soinside.com 2019 - 2024. All rights reserved.