【问题标题】:How to query an advanced search with google customsearch API?如何使用 google customsearch API 查询高级搜索?
【发布时间】: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 控制台中的外观:

credentials

【问题讨论】:

  • 你得到什么错误?
  • @EugeneLisitsky,我没有收到任何错误。问题是我不明白如何使用谷歌的 API 制作 advanced search。例如,我如何以编程方式使用 google 查询所有在 english 中包含 the best dog foodurls UK

标签: python python-3.x google-api google-search-api google-api-python-client


【解决方案1】:

如果您不想使用 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())

【讨论】:

  • 感谢它的工作,但是为什么当我们传递多个单词的查询时没有检索到:“valencia party”.. ?
【解决方案2】:

这已经很晚了,但希望它可以帮助某人......

高级搜索使用

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

list() 方法采用更多参数来帮助推进搜索...在此处查看参数: https://developers.google.com/custom-search/json-api/v1/reference/cse/list

【讨论】:

    【解决方案3】:

    首先,您需要按照here 的描述定义自定义搜索,然后确保您的my_cse_id 与google API custom search (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()
    

    我们可以使用here 描述的参数添加其他搜索参数,例如语言或国家/地区,例如

    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/
    

    【讨论】:

    • 感谢您的帮助!但是,我的问题是要提出一个advanced search(即用特定的短语、单词、地区、域、语言等进行谷歌查询)。我的主要目标是以编程方式进行高级搜索。
    • 另外,我不明白为什么您的代码示例只返回 CS 讲座链接而不是狗链接。你能告诉我们如何用英语对西雅图拳击犬的所有网址进行高级搜索吗?
    • 感谢您的澄清!请参阅更新后的答案,加拿大的拳师犬会说英语。
    • 谢谢,这就是我想做的。现在从上面的示例中产生了几个问题。为什么当我设置num=90 时我得到:HttpError: &lt;HttpError 400 when requesting https://www.googleapis.com/customsearch
    • 来自文档:有效值是 1 到 10 之间的整数,包括 1 和 10。所有参数都在这里:developers.google.com/custom-search/json-api/v1/reference/cse/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2016-03-27
    相关资源
    最近更新 更多