【问题标题】:Why is my http.client request in Python returning [gaierror: [Errno 11001] getaddrinfo failed]?为什么我在 Python 中的 http.client 请求返回 [gaierror: [Errno 11001] getaddrinfo failed]?
【发布时间】:2023-02-10 01:23:18
【问题描述】:

我正在尝试使用 API 从 https://epc.opendatacommunities.org/docs/api/domestic 中提取 EPC 评级数据。

这是我的代码:

from http.client import HTTPSConnection
from base64 import b64encode

# Authorization token: we need to base 64 encode it 
# and then decode it to acsii as python 3 stores it as a byte string
def basic_auth(username, password):
    token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii")
    return f'Basic {token}'

username = "ethan.mercer@aecom.com"
password = "bc6b6b549004ec082a1b8a3a03bd9d1adde3f02b"

#This sets up the https connection
c = HTTPSConnection("epc.opendatacommunities.org/api/v1/domestic/search")
#then connect
headers = { 'Authorization' : basic_auth(username, password) }
c.request('GET', '/', headers=headers)
#get the response back
res = c.getresponse()
# at this point you could check the status etc
# this gets the page text
data = res.read()  

返回 EPC 评级的结果。

【问题讨论】:

    标签: python api http request


    【解决方案1】:

    您已将连接 host 参数设置为 "epc.opendatacommunities.org/api/v1/domestic/search",并将请求 URL 设置为 "/"。主机应为"epc.opendatacommunities.org",URL 应为"/api/v1/domestic/search"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 2018-02-12
      • 2021-10-05
      • 1970-01-01
      • 2021-05-27
      相关资源
      最近更新 更多