brady-wang

总体功能的一个演示

复制代码
import requests

response  = requests.get("https://www.baidu.com")
print(type(response))
print(response.status_code)
print(type(response.text))
print(response.text)
print(response.cookies)
print(response.content)
print(response.content.decode("utf-8"))
复制代码

我们可以看出response使用起来确实非常方便,这里有个问题需要注意一下:
很多情况下的网站如果直接response.text会出现乱码的问题,所以这个使用response.content
这样返回的数据格式其实是二进制格式,然后通过decode()转换为utf-8,这样就解决了通过response.text直接返回显示乱码的问题.

请求发出后,Requests 会基于 HTTP 头部对响应的编码作出有根据的推测。当你访问 response.text 之时,Requests 会使用其推测的文本编码。你可以找出 Requests 使用了什么编码,并且能够使用 response.encoding 属性来改变它.如:

response =requests.get("http://www.baidu.com")
response.encoding="utf-8"
print(response.text)

不管是通过response.content.decode("utf-8)的方式还是通过response.encoding="utf-8"的方式都可以避免乱码的问题发生

分类:

技术点:

相关文章:

  • 2022-01-09
  • 2021-06-02
  • 2021-06-09
  • 2021-04-18
  • 2021-10-05
  • 2021-12-31
  • 2021-09-19
猜你喜欢
  • 2021-09-04
  • 2022-02-14
  • 2022-01-26
  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
  • 2021-10-02
相关资源
相似解决方案