我正在调用一个API,它返回的是字典列表,但以字节类型表示。我尝试解码,但最后用字符串而不是字典列表结束。我不确定如何解码它,并拥有字典列表,然后可以将其写入数据框或CSV。
我的代码如下所示(我隐藏了订阅密钥):
headers = {
# Request headers
'Ocp-Apim-Subscription-Key': '***',
}
params = urllib.parse.urlencode({
})
try:
conn = http.client.HTTPSConnection('api.fantasydata.net')
conn.request("GET", "/nascar/v2/json/drivers?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
输出为(缩短):
b'[{
"DriverID":80000544,
"FirstName":"Jeffrey",
"LastName":"Abbey",
"Number":34,
"Team":null,
"BirthDate":null,
"BirthPlace":null,
"Gender":null,
"Height":null,
"Weight":null,
"Manufacturer":null,
"Engine":null,
"Chassis":null,
"Sponsors":null,
"CrewChief":null,
"PhotoUrl":"https:\\/\\/s3-us-west-2.amazonaws.com\\/static.fantasydata.com\\/headshots\\/nas\\/low-res\\/0.png",
"Updated":"2018-07-14T03:17:41",
"Created":"2017-07-15T09:38:41"}, ...