gpt4 book ai didi

python - 使用 Python 通过 Azure API 搜索 Bing

转载 作者:太空狗 更新时间:2023-10-29 22:22:13 24 4
gpt4 key购买 nike

如何使用关键字在 Bing 中搜索图像?

我可以使用以下方式搜索 Google:

import urllib2
import json

credentialGoogle = '' # Google credentials from: https://console.developers.google.com/
searchString = 'Xbox%20One'
top = 20
offset = 0
while offset < top:
url = 'https://ajax.googleapis.com/ajax/services/search/images?' + \
'v=1.0&q=%s&start=%d&userip=%s' % (searchString, offset, credentialGoogle)

request = urllib2.Request(url)
response = urllib2.urlopen(request)
results = json.load(response)

# process results

offset += 4 # since Google API only returns 4 search results at a time

Bing 的等效项是什么?大概是这样开始的:

keyBing = ''        # Bing key from: https://datamarket.azure.com/account/keys
credentialBing = '' # same as key?
searchString = '%27Xbox+One%27'
top = 20
offset = 0

url = 'https://api.datamarket.azure.com/Bing/Search/Image?' + \
'Query=%s&$top=%d&$skip=%d&$format=json' % (searchString, top, offset)

但是凭据是如何设置的?

最佳答案

Bing 的等效项是:

keyBing = ''        # get Bing key from: https://datamarket.azure.com/account/keys
credentialBing = 'Basic ' + (':%s' % keyBing).encode('base64')[:-1] # the "-1" is to remove the trailing "\n" which encode adds
searchString = '%27Xbox+One%27'
top = 20
offset = 0

url = 'https://api.datamarket.azure.com/Bing/Search/Image?' + \
'Query=%s&$top=%d&$skip=%d&$format=json' % (searchString, top, offset)

request = urllib2.Request(url)
request.add_header('Authorization', credentialBing)
requestOpener = urllib2.build_opener()
response = requestOpener.open(request)

results = json.load(response)

# process results

解决方案感谢:http://www.guguncube.com/2771/python-using-the-bing-search-api

关于python - 使用 Python 通过 Azure API 搜索 Bing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27606478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com