解析HTTP响应为JSON

2024-05-16 08:58:59 发布

您现在位置:Python中文网/ 问答频道 /正文

抱歉的noob问题,但需要帮助解析HTTP响应,我收到后的请求。你知道吗

例如:

import requests
import json
import base64
url = 'someURL'
if __name__=='__main__':
    data = '{"requests":"data"}'
    r = requests.post(url, data)
    print(r.content)
    print(r.encoding)

我收到下一个:

b'{"results":[{"hits":[{"aliases":["Defence of the Ancients 2","Defense of the Ancients 2","\xe0\xbc\xbc \xe3\x81\xa4 \xe2\x97\x95_\xe2\x97\x95 \xe0\xbc\xbd\xe3\x81\xa4"],"localizations":{},"name":"Dota 2","popularity":36184,"objectID":"29595","highlightResult":{"aliases":[{"value":"Defence of the Ancients 2","matchLevel":"none","matchedWords":[]},{"value":"Defense of the Ancients 2","matchLevel":"none","matchedWords":[]},{"value":"\xe0\xbc\xbc \xe3\x81\xa4 \xe2\x97\x95\xe2\x97\x95 \xe0\xbc\xbd\xe3\x81\xa4","matchLevel":"none","matchedWords":[]}],"name":{"value":"Dota 2","matchLevel":"full","fullyHighlighted":false,"matchedWords":["dota"]}}},{"aliases":["Blizzard All-Stars","Blizzard DOTA","HOTS"],"localizations":{},"name":"Heroes of the Storm","popularity":27143,"objectID":"32959","_highlightResult":{"aliases":[{"value":"Blizzard All-Stars","matchLevel":"none","matchedWords":[]},{"value":"Blizzard DOTA","matchLevel":"full","fullyHighlighted":false,"matchedWords":["dota"]},{"value":"HOTS","matchLevel":"none","matchedWords":[]}],"name":{"value":"Heroes of the Storm","matchLevel":"none","matchedWords":[]}}},{"aliases":[],"localizations":{},"name":"Fujiko F. Fujio Characters Daishuugou! SF Dotabata Party!!","popularity":0,"objectID":"490150","_highlightResult":{"name":{"value":"Fujiko F. Fujio Characters Daishuugou! SF Dotabata Party!!","matchLevel":"full","fullyHighlighted":false,"matchedWords":["dota"]}}},{"aliases":[],"localizations":{},"name":"\xe5\x88\x80\xe5\xa1\x94\xe5\x82\xb3\xe5\xa5\x87 Dota Legend","popularity":0,"objectID":"488497","_highlightResult":{"name":{"value":"\xe5\x88\x80\xe5\xa1\x94\xe5\x82\xb3\xe5\xa5\x87 Dota Legend","matchLevel":"full","fullyHighlighted":false,"matchedWords":["dota"]}}}],"nbHits":4,"page":0,"nbPages":1,"hitsPerPage":1000,"processingTimeMS":1,"facets":{},"exhaustiveFacetsCount":true,"exhaustiveNbHits":true,"query":"dota","params":"query=dota&page=0&hitsPerPage=99999&numericFilters=%5B%5D&facets=*&facetFilters=","index":"game"}]}\n'

UTF-8

但我如何能尝试编码,格式化它仍然有一个不同的问题。请帮助,这个请求如何得到有效的json?你知道吗

我的坏例子有:

print(j.json())
Traceback (most recent call last):
  File "./get_games.py", line 14, in <module>
    print(r.json())
UnicodeEncodeError: 'ascii' codec can't encode character '\u0f3c' in position 95: ordinal not in range(128)

或者这个:

json.loads(str(r.content)[1:])
Traceback (most recent call last):
  File "./get_games.py", line 17, in <module>
    a = json.loads(str(r.content)[1:])
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

谢谢


Tags: ofthenameinnonejsonvalueline
1条回答
网友
1楼 · 发布于 2024-05-16 08:58:59

报告中有些奇怪的地方,因为打印j.json()会导致报告中的r.json()出错。。。。你知道吗

如果不使用str()进行隐式解码,第二次尝试就可以了。你知道吗

试着用明确的、完全冗长的方式:

print(json.loads(r.content.decode(r.encoding)))

相关问题 更多 >