【问题标题】:Bing Congitive Web Search API with Python 3使用 Python 3 的 Bing Congitive Web 搜索 API
【发布时间】:2016-07-02 05:38:07
【问题描述】:

你好 Stackoverflow 社区

我正在尝试通过 Python 3 脚本访问新的 Bing 认知搜索 API。我能够发现使用 Bing Search 2.0(自已弃用)的威胁,但无法确定使用 Python 3 的新 API 的示例。我使用了以下代码:

import urllib.parse
import urllib.request
import json
import base64


def bing_search(query):
    key = 'mysubscription_key'
    query = urllib.parse.quote(query)

    #Create credentials for authentication
    user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; FDM; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 1.1.4322)'
    encoded = base64.b64encode(bytes(':%s' % key, 'utf-8'))
    credentials = encoded[:-1]   # the "-1" is to remove the trailing "\n" which encode adds
    print(credentials)
    auth = 'Basic %s' % credentials
    print(auth)
    url = 'https://api.cognitive.microsoft.com/bing/v5.0/search?q=' + query + '&mkt=en-us'
    print(url)

    #Create the API request
    urlrequest = urllib.request.urlopen(url)        # in Python3 urllib.request(...) becomes urllib.request.open(...)
    urlrequest.add_header('Authentication', auth)
    urlrequest.add_header('User Agent', user_agent)
    request_opener = urllib.request.build_opener()

    # Handle the response
    response = request_opener.open(urlrequest)
    results = json.load(response)

    result_list = results['webPages']['values']
    print(result_list)

bing_search('good news')

很遗憾,我收到以下“拒绝访问”错误。有人能指出我正确的方向吗?

C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/Admin/PycharmProjects/momely/placementarchitect/bingtest.py
b'OmZhNTlmNTBlMmFmMjQyZjhhYmE5MTZlNmZkYThhMDM'
Basic b'OmZhNTlmNTBlMmFmMjQyZjhhYmE5MTZlNmZkYThhMDM'
https://api.cognitive.microsoft.com/bing/v5.0/search?q=good%20news&mkt=en-us
Traceback (most recent call last):
  File "C:/Users/Admin/PycharmProjects/momely/placementarchitect/bingtest.py", line 34, in <module>
    bing_search('good news')
  File "C:/Users/Admin/PycharmProjects/momely/placementarchitect/bingtest.py", line 22, in bing_search
    urlrequest = urllib.request.urlopen(url)        # in Python3 urllib.request(...) becomes urllib.request.open(...)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 471, in open
    response = meth(req, response)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 581, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 509, in error
    return self._call_chain(*args)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain
    result = func(*args)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 589, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Access Denied

谢谢你 M.

【问题讨论】:

    标签: python python-3.x urllib bing


    【解决方案1】:

    身份验证不是通过基本身份验证,而是通过自定义标头Ocp-Apim-Subscription-Key

    提供对该 API 的访问的订阅密钥。

    可以在您的subscriptions 中找到它

    https://www.microsoft.com/cognitive-services/en-us/bing-web-search-api查看完整详情

    假设key变量持有订阅密钥,你需要做的只是urlrequest.add_header('Ocp-Apim-Subscription-Key', key)

    作为一个小建议,您的代码可以通过使用python-requests 大大简化。一个最小的例子可以是:

    import requests
    
    def bing_search(query):
        url = 'https://api.cognitive.microsoft.com/bing/v5.0/search'
        # query string parameters
        payload = {'q': query}
        # custom headers
        headers = {'Ocp-Apim-Subscription-Key': 'xxxxxxxxxxxxx'}
        # make GET request
        r = requests.get(url, params=payload, headers=headers)
        # get JSON response
        return r.json()
    
    j = bing_search('good news')
    print(j.get('webPages', {}).get('value', {}))
    

    【讨论】:

    • 谢谢你woozyking。不幸的是,我仍然收到相同的错误:urllib.error.HTTPError: HTTP Error 401: Access Denied
    • 你到底做了什么改变?
    • 非常感谢您的请求替代方案运作良好。真的很感激。
    • 没问题,很高兴它成功了(很高兴你使用requests
    【解决方案2】:

    您还应该查看适用于 Python 的 py-ms-cognitive 包。它是认知服务 API 的包装器,目前支持网络搜索。我也在寻找反馈/合作者,所以请随时留下问题,进行 PR 等。

    安装:

    pip install py-ms-cognitive
    

    使用方法:

    from py_ms_cognitive import PyMsCognitiveWebSearch
    web_bing = PyMsCognitiveWebSearch('api_key', "xbox")
    first_fity_results = web_bing.search(limit=50)
    second_fity_results = web_bing.search(limit=50)
    print second_fifty_results[0].url 
    

    【讨论】:

      猜你喜欢
      • 2013-07-13
      • 1970-01-01
      • 2014-11-04
      • 2018-09-30
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多