Python response.text与content区别

    从网络请求下来的数据,他们都是字节类型的;如果服务器不指定的话,默认编码是"ISO-8859-1";我们使用text直接拿到的是字符串类型,没有进行解码操作,则会出现乱码问题。

    resp_ = requests.get(url_)
    print(resp_.encoding)

 

 (1)response.text 字符串类型

    print(resp_.text)
    print(type(resp_.text))

 解决乱码:可以直接使用content得到字节类型的数据再解码

    print(resp_.content.decode("utf-8"))
    print(type(resp_.content))

Python response.text与content区别_第1张图片

 (2)response.content 字节类型

    print(resp_.content)
    print(type(resp_.content))

总之:

  • resp_.text返回的是Unicode型的数据。
  • resp_.content返回的是bytes型也就是二进制的数据。

你可能感兴趣的:(Python,java,开发语言)