Urllib2 和 JSON 对象错误

Urllib2 and JSON Object Error

我正在关注 tutorial 在我的项目中添加 'Search Function'。但是,本教程基于 Python2x & Django 1.7,而我使用的是 Python 3.4 & Django 1.7。

搜索代码使用 bing_search 和 urllib2。然而 urllib2 在 Python 3 中不受支持,并且已在几个后续模块中分发。

一些我如何设法让我的代码与 Python 兼容的方法 3. 但是现在我在提交搜索查询时遇到以下错误。

The JSON object must be str, not 'bytes'

这是我的代码供您查看:

import json
import urllib
import urllib.request
from urllib.parse import quote
import urllib.error

# Add your BING_API_KEY

BING_API_KEY = 'key removed for the sake of privacy'

def run_query(search_terms):
    # Specify the base
    root_url = 'https://api.datamarket.azure.com/Bing/Search/'
    source = 'Web'

    # Specify how many results we wish to be returned per page.
    # Offset specifies where in the results list to start from.
    # With results_per_page = 10 and offset = 11, this would start from page 2.
    results_per_page = 10
    offset = 0

    # Wrap quotes around our query terms as required by the Bing API.
    # The query we will then use is stored within variable query.
    query = "'{0}'".format(search_terms)
    query = urllib.parse.quote(query)

    # Construct the latter part of our request's URL.
    # Sets the format of the response to JSON and sets other properties.
    search_url = "{0}{1}?$format=json&$top={2}&$skip={3}&Query={4}".format(
        root_url,
        source,
        results_per_page,
        offset,
        query)

    # Setup authentication with the Bing servers.
    # The username MUST be a blank string, and put in your API key!
    username = ''


    # Create a 'password manager' which handles authentication for us.
    password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None, search_url, username, BING_API_KEY)

    # Create our results list which we'll populate.
    results = []

    try:
        # Prepare for connecting to Bing's servers.
        handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
        opener = urllib.request.build_opener(handler)
        urllib.request.install_opener(opener)

        # Connect to the server and read the response generated.
        response = urllib.request.urlopen(search_url).read()

        # Convert the string response to a Python dictionary object.
        json_response = json.loads(response)

        # Loop through each page returned, populating out results list.
        for result in json_response['d']['results']:
            results.append({
            'title': result['Title'],
            'link': result['Url'],
            'summary': result['Description']})

    # Catch a URLError exception - something went wrong when connecting!
    except urllib.error.URLError as e:
        print("Error when querying the Bing API: ", e)

    # Return the list of results to the calling function.
    return results

请指导我现在应该做什么。

提前谢谢你....!

PS: 我看过一些类似的帖子,但我无法解决问题。

您需要在将响应传递给 json 模块之前对其进行解码:

response = urllib.request.urlopen(search_url)
codec = response.info().get_param('charset', 'utf8')  # default JSON encoding
json.loads(response.read().decode(codec))

服务器旨在为您提供 Content-Type header 中的 charset=... 参数,其中 message.get_param() method above would retrieve for you. If the parameter is absent, the code falls back to UTF-8, the default encoding for JSON as per the RFC.