使用Python通过Azure API搜索Bing

2024-09-27 07:19:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我可以使用以下方式搜索谷歌:

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)

但是如何设置凭据?


Tags: fromhttpsimportcomjsonurltopgoogle
3条回答

有一个名为PyBingSearch的python包(好吧,我承认,我写了一大块包)。

要安装:

pip install py-bing-search

在Python2.*.*中:

from py_bing_search import PyBingImageSearch
bing_image = PyBingImageSearch('Your-Api-Key-Here', "x-box console")
first_fifty_results = bing_image.search(limit=50, format='json') #1-50
print (first_fifty_results[0].media_url)

你需要从Bing获得API密钥(每月免费5公里)。该软件包允许您搜索网络、图像、视频和新闻。

现在适用于Python3。*同样,只需使用以下命令安装:

pip3 install py-bing-search

现在有一个新的Microsoft Cognitive Service接管了旧的API。可以帮助您实现这一点的新python包是PyMsCognitive

在Python3.0+中,这看起来像:

from urllib.parse import quote_plus
import json
import requests

def bing_search(query):
    # Your base API URL; change "Image" to "Web" for web results.
    url = "https://api.datamarket.azure.com/Bing/Search/v1/Image"

    # Query parameters. Don't try using urlencode here.
    # Don't ask why, but Bing needs the "$" in front of its parameters.
    # The '$top' parameter limits the number of search results.
    url += "?$format=json&$top=10&Query=%27{}%27".format(quote_plus(query))

    # You can get your primary account key at https://datamarket.azure.com/account
    r = requests.get(url, auth=("","YOUR_AZURE_API_PRIMARY_ACCOUNT_KEY"))
    resp = json.loads(r.text)
    return(resp)

这是基于我的网络搜索功能here

必应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

相关问题 更多 >

    热门问题