如何使用google customsearch API查询高级搜索? [英] How to query an advanced search with google customsearch API?

查看:233
本文介绍了如何使用google customsearch API查询高级搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式使用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)

表示结果的结果:
pprint.pprint(结果)



$ p
$ b pre $ p $ import pre $

from googleapiclient.discovery import build

$ b 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执行高级搜索?这是我的凭据如何看待谷歌控制台:



定义自定义搜索。 //developers.google.com/custom-search/docs/apirel =nofollow noreferrer> here ,然后确保您的 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)

现在我们可以执行我们的搜索。
$ b $ pre $ res = service.cse().list(q = search_term,cx = my_cse_id).execute()

我们可以使用参数这里,例如

  res = service.cse()。list(q =最好的狗食,cx = my_cse_id,cr =countryUK,lr =lang_en)。执行()

会用英文表示最好的狗食,并且网站需要来自英国。




以下修改后的代码适用于我。 api_key 已被删除,因为它从未被使用过。

  from googleapiclient.discovery import build 

my_cse_id =012156694711735292392:rl7x1k3j0vy
dev_key =< Your developer key>
$ b $ 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,
print(result.get('link'))
结果结果: >

输出
$ b


  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/



How can I programmatically using the Google Python client library do an advanced search with Google custom search API search engine in order to return a list of first n links based in some terms and parameters of an advanced search I queried?.

I tried to check the documentation(I did not found any example), and this answer. However, the latter did not worked, since currently there is no support for the AJAX API. So far I tried this:

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)

And this:

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()

Thus, any idea of how to do and advanced search with google's search engine API?. This is how my credentials look at google console:

credentials

解决方案

First you need to define a custom search as described here, then make sure your my_cse_id matches the google API custom search (cs) id, e.g.

cx='017576662512468239146:omuauf_lfve'

is a search engine which only searches for domains ending with .com.

Next we need our developerKey.

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

Now we can execute our search.

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

We can add additional search parameters, like language or country by using the arguments described here, e.g.

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

would serch for "the best dog food" in English and the site needs to be from the UK.


The following modified code worked for me. api_key was removed since it was never used.

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'))

Output

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/

这篇关于如何使用google customsearch API查询高级搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆