python中content什么意思_python中requests的response.text与response.content区别

原地址:https://blog.csdn.net/qq_31362767/article/details/81431124

requestsimport requests 

response=requests.get(“http://www.baidu.com/“)

一、response的属性:print(response)

response.status_code

http请求的返回状态,2XX 表示连接成功,3XX 表示跳转 ,4XX 客户端错误 , 500 服务器错误

response.text

http响应内容的字符串(str)形式,请求url对应的页面内容response=requests.get("http://www.baidu.com/")

print(response.text)

打印出的内容会含有乱码

修改如下, 改变下载得到的页面的编码,就可以正常打印出”友好的”文本了:response.encoding="utf-8"

print(response.text)

小结 : 更改编码使用:response.encoding=”utf-8” 或者 response.encoding=”gbk”

具体要看你请求的网页是用什么方式编码的,针对不同情况用对应的编码方式.

response.content

HTTP响应内容的二进制(bytes)形式response =requests.get("http://www.baidu.com/")

# print(response.content) #打印出的是二进制形式

print(response.content.decode("utf-8"))

小结:更改编码使用 response.content.deocde(“utf8”)

更推荐使用response.content.deocde()的方式获取响应的html页面.

response.encoding

从HTTP header中猜测的响应内容编码方式

response.apparent_encoding

从内容分析出的响应内容的编码方式(备选编码方式)

response.headers

http响应内容的头部内容

本文章修改于 2019/12/05 22:49:40

标签:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值