一直不清楚requestes的content和text方法的区别,只知道content返回的是二进制数据,而text返回的是文本数据

 

先看看content的源码:

python request中的content和text的区别

注释可知content返回的是bytes型也就是二进制的数据

在看看text的源码:

python request中的content和text的区别

意思是requests.text是根据网页的响应来猜测编码,如果服务器不指定的话,默认编码是"ISO-8859-1"所以这是为什么有些时候用 response.text 返回的是乱码的原因。

可以用response.encoding看一下他猜测的编码是啥。然后用response.encoding = 'utf-8'来设置编码

实例如下:

1 import requests
2 response=requests.get('http://www.qq.com')
3 response.encoding
4 >>'GB2312'
View Code

相关文章:

  • 2022-03-09
  • 2022-12-23
  • 2021-06-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-22
  • 2021-07-01
  • 2021-10-06
  • 2022-01-07
  • 2021-10-30
  • 2021-06-27
  • 2021-06-30
相关资源
相似解决方案