Django Bing API错误:未提供主机

Django Bing API错误:未提供主机,django,python-3.x,bing-api,Django,Python 3.x,Bing Api,我在尝试从Bing加载搜索结果时遇到问题。我试着寻找解决方案,但我不知道该怎么办。 我用#API key此处将我的API key隐藏在代码段中,但它应该是正确的 以下是我的代码片段: import json import urllib.request, urllib.parse, urllib.error import urllib.request, urllib.error, urllib.parse def run_query(search_terms): # Specify th

我在尝试从Bing加载搜索结果时遇到问题。我试着寻找解决方案,但我不知道该怎么办。 我用
#API key此处
将我的API key隐藏在代码段中,但它应该是正确的

以下是我的代码片段:

import json
import urllib.request, urllib.parse, urllib.error
import urllib.request, urllib.error, urllib.parse

def run_query(search_terms):
    # Specify the base
    root_url = "https:a//pi.cognitive.microsoft.com/bing/v7.0/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 = ''
bing_api_key = #api key here

# 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 Exception as e:
    print(("Error when querying the Bing API: ", e))

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

错误是:
(“查询Bing API时出错:”,URLError(“未提供主机”)

根url中有一个输入错误。它应该是:。

我建议您有一个调试标志,它将在激活时重新引发异常,这样您就可以确切地知道是哪一行导致了问题。然后,如果不清楚,在这里发布。