如何使用 google customsearch API 查询高级搜索?
How to query an advanced search with google customsearch API?
如何以编程方式使用 Google Python 客户端库执行 advanced search 和 Google 自定义搜索 API 搜索引擎,以便 return 基于我查询的高级搜索的某些术语和参数的第一个 n
链接列表?。
我试图检查 documentation(I did not found any example), and this 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 console:
credentials
首先您需要按照here, then make sure your my_cse_id
matches the 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()
会用英语搜索 "the best dog food" 并且网站需要来自英国。
以下修改后的代码对我有用。 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/
来晚了,但希望对某人有所帮助...
用于高级搜索
response=service.cse().list(q="mysearchterm",
cx="017576662512468239146:omuauf_lfve", ).execute()
list()
方法采用更多参数来帮助推进搜索...在此处检查参数:
https://developers.google.com/custom-search/json-api/v1/reference/cse/list
如果您不想使用 google 发现 api:
,则可以使用 python 请求库
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())
如何以编程方式使用 Google Python 客户端库执行 advanced search 和 Google 自定义搜索 API 搜索引擎,以便 return 基于我查询的高级搜索的某些术语和参数的第一个 n
链接列表?。
我试图检查 documentation(I did not found any example), and this 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 console:
credentials
首先您需要按照here, then make sure your my_cse_id
matches the 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()
会用英语搜索 "the best dog food" 并且网站需要来自英国。
以下修改后的代码对我有用。 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/
来晚了,但希望对某人有所帮助...
用于高级搜索
response=service.cse().list(q="mysearchterm",
cx="017576662512468239146:omuauf_lfve", ).execute()
list()
方法采用更多参数来帮助推进搜索...在此处检查参数:
https://developers.google.com/custom-search/json-api/v1/reference/cse/list
如果您不想使用 google 发现 api:
,则可以使用 python 请求库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())