Request returns bytes and I'm failing to decode them

蓝咒 提交于 2019-11-28 23:25:27

Did you try to parse it with the json module?

import json
parsed = json.loads(response.content)

Another solution is to use response.text, which returns the content in unicode

Type:        property
String form: <property object at 0x7f76f8c79db8>
Docstring:  
Content of the response, in unicode.

If Response.encoding is None, encoding will be guessed using
``chardet``.

The encoding of the response content is determined based solely on HTTP
headers, following RFC 2616 to the letter. If you can take advantage of
non-HTTP knowledge to make a better guess at the encoding, you should
set ``r.encoding`` appropriately before accessing this property.

There is r.text and r.content. The first one is a string, the second one is bytes.

You want

import json

data = json.loads(r.text)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!