主页 > 知识库 > python中response.text 和response.content的区别详解

python中response.text 和response.content的区别详解

热门标签:沈阳人工外呼系统价格 沈阳外呼系统呼叫系统 沈阳防封电销卡品牌 如何申请400电话费用 富锦商家地图标注 江西省地图标注 池州外呼调研线路 武汉外呼系统平台 外呼系统哪些好办

1.response.text

- 类型:str

- 解码类型: 根据HTTP 头部对响应的编码作出有根据的推测,推测的文本编码

- 如何修改编码方式:response.encoding=”gbk”

2. response.content

- 类型:bytes

- 解码类型: 没有指定

- 如何修改编码方式:response.content.deocde(“utf-8”)

3.获取网页源码的通用方式:

response.content.decode()
response.content.decode(“GBK”)

解码方式可以根据响应头中找到Content-Type:text/html;charset=utf-8或者网页源码中content="text/html;charset=utf-8''来决定.

response.text

以上三种方法从前往后尝试,能够100%的解决所有网页解码的问题

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

补充:python3中requests 常用response

看代码吧~

import requests
res = requests.get("http://127.0.0.1:9092")

print(res.status_code) # 200
print(res.url) # http://127.0.0.1:9092/
print(res.headers) # {'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '10', 'Date': 'Sat, 22 Dec 2018 13:36:16 GMT', 'Connection': 'keep-alive'}
print(res.cookies) # RequestsCookieJar[Cookie cid=hello world for 127.0.0.1/>]>
print(res.text) # 8248154254
print(res.content) # b'8248154254' 写图片
print(res.cookies['cid']) # hello world

# 爬取 图片
r1 = requests.get('https://timgsa.baidu.com/timg?imagequality=80size=b9999_10000sec=1545496551516di=8ed19596bc72aa87299ed7f234830128imgtype=0src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201107%2F31%2F20110731155631_htMcs.jpg')
b = r1.content
with open('hao.jpg','wb') as f:
    f.write(b)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
  • 如何使用spring ResponseEntity处理http响应
  • jsp Response对象页面重定向、时间的动态显示
  • JavaWeb入门:HttpResponse和HttpRequest详解
  • Flask response响应的具体使用
  • ResponseBodyAdvice踩坑及解决
  • 基于@RequestBody和@ResponseBody及Stringify()的作用说明
  • SpringMVC注解之@ResponseBody注解原理
  • Response.AddHeader案例讲解

标签:常德 株洲 阿里 铜川 通辽 黑龙江 潜江 吕梁

巨人网络通讯声明:本文标题《python中response.text 和response.content的区别详解》,本文关键词  python,中,response.text,和,response.content,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《python中response.text 和response.content的区别详解》相关的同类信息!
  • 本页收集关于python中response.text 和response.content的区别详解的相关信息资讯供网民参考!
  • 推荐文章