目的:复习常用的response对象的属性

import requests

url = "http://www.baidu.com"

r = requests.get(url)

print(r.status_code) #状态码

print(r.content) #响应内容为二进制形式

print(r.text) #响应内容为字符串形式,但是print出来是乱码

print(r.encoding) #返回的编码方式为"ISO-8859-1",不认识

print(r.apparent_encoding) #返回的编码方式为"utf-8",认识

 

r.encoding = 'utf-8'

print(r.text) #print出来不是乱码,因为变更了encoding方式

 

总结两种编码

r.encoding:从header中猜测的响应方式,比如百度……header中就没有charset字段,会直接默认了"ISO-8859-1"

r.apparent_encoding:分析内容找到的,很实在

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
  • 2021-12-17
  • 2022-01-06
猜你喜欢
  • 2021-12-22
  • 2022-01-24
  • 2021-11-03
  • 2022-01-09
  • 2021-11-02
相关资源
相似解决方案