对python requests的content和text方法的区别详解


Posted in Python onOctober 11, 2018

问题:

一直在想requests的content和text属性的区别,从print 结果来看是没有任何区别的

看下源码:

@property
  def text(self):
    """Content of the response, in unicode.

    If Response.encoding is None, encoding will be guessed using
    ``chardet``.

    The encoding of the response content is determined based solely on HTTP
    headers, following RFC 2616 to the letter. If you can take advantage of
    non-HTTP knowledge to make a better guess at the encoding, you should
    set ``r.encoding`` appropriately before accessing this property.
    """

  #content的完整代码就不贴了。
  @property
  def content(self):
    """Content of the response, in bytes."""

结论是:

resp.text返回的是Unicode型的数据。

resp.content返回的是bytes型也就是二进制的数据。

也就是说,如果你想取文本,可以通过r.text。

如果想取图片,文件,则可以通过r.content。

(resp.json()返回的是json格式数据)

举个栗子

# 例如下载并保存一张图片

import requests

jpg_url = 'http://img2.niutuku.com/1312/0804/0804-niutuku.com-27840.jpg'

content = requests.get(jpg_url).content

with open('demo.jpg', 'wb') as fp:
  fp.write(content)

以上这篇对python requests的content和text方法的区别详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python抓取某汽车网数据解析html存入excel示例
Dec 04 Python
你所不知道的Python奇技淫巧13招【实用】
Dec 14 Python
Python读取word文本操作详解
Jan 22 Python
python实现泊松图像融合
Jul 26 Python
解决每次打开pycharm直接进入项目的问题
Oct 28 Python
对python中dict和json的区别详解
Dec 18 Python
浅析Python 读取图像文件的性能对比
Mar 07 Python
Python3.5集合及其常见运算实例详解
May 01 Python
python实现BP神经网络回归预测模型
Aug 09 Python
sklearn-SVC实现与类参数详解
Dec 10 Python
Python 3.8 新功能大揭秘【新手必学】
Feb 05 Python
DjangoRestFramework 使用 simpleJWT 登陆认证完整记录
Jun 22 Python
使用pip发布Python程序的方法步骤
Oct 11 #Python
对python Tkinter Text的用法详解
Oct 11 #Python
python数据批量写入ScrolledText的优化方法
Oct 11 #Python
攻击者是如何将PHP Phar包伪装成图像以绕过文件类型检测的(推荐)
Oct 11 #Python
python中join()方法介绍
Oct 11 #Python
Python中staticmethod和classmethod的作用与区别
Oct 11 #Python
对Python 窗体(tkinter)文本编辑器(Text)详解
Oct 11 #Python
You might like
2020最新CPU的性能排名
2020/04/02 数码科技
模板引擎Smarty深入浅出介绍
2006/12/06 PHP
php调用mysql数据 dbclass类
2011/05/07 PHP
ThinkPHP实现转换数据库查询结果数据到对应类型的方法
2017/11/16 PHP
phpinfo无法显示的原因及解决办法
2019/02/15 PHP
PHP的介绍以及优势详细分析
2019/09/05 PHP
Mootools 1.2教程 选项卡效果(Tabs)
2009/09/15 Javascript
用jquery生成二级菜单的实例代码
2013/06/24 Javascript
JavaScript中的undefined学习总结
2013/11/30 Javascript
jquery和javascript中如何将一元素的内容赋给另一元素
2014/01/09 Javascript
Node.js返回JSONP详解
2016/05/18 Javascript
easyui导出excel无法弹出下载框的快速解决方法
2016/11/10 Javascript
关于js函数解释(包括内嵌,对象等)
2016/11/20 Javascript
使用jquery datatable和bootsrap创建表格实例代码
2017/03/17 Javascript
laypage.js分页插件使用方法详解
2019/07/27 Javascript
layui 监听select选择 获取当前select的ID名称方法
2019/09/24 Javascript
[36:52]DOTA2真视界:基辅特锦赛总决赛
2017/05/21 DOTA
解决python3 urllib中urlopen报错的问题
2017/03/25 Python
对python sklearn one-hot编码详解
2018/07/10 Python
Django 实现admin后台显示图片缩略图的例子
2019/07/28 Python
python3.7实现云之讯、聚合短信平台的短信发送功能
2019/09/26 Python
pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法)
2020/04/15 Python
使用Python将语音转换为文本的方法
2020/08/10 Python
Django filter动态过滤与排序实现过程解析
2020/11/26 Python
matplotlib部件之矩形选区(RectangleSelector)的实现
2021/02/01 Python
python 制作本地应用搜索工具
2021/02/27 Python
使用CSS3的appearance属性改变元素的外观的方法
2015/12/12 HTML / CSS
css3实现书本翻页效果的示例代码
2021/03/08 HTML / CSS
澳大利亚家具和家居用品在线:BROSA
2017/11/02 全球购物
印度在线购物网站:Paytmmall
2019/07/24 全球购物
如何处理简单的PHP错误
2015/10/14 面试题
杭州时比特电子有限公司SQL
2013/08/22 面试题
新领导上任欢迎词
2014/01/13 职场文书
2014年酒店工作总结范文
2014/11/17 职场文书
2015年教学副校长工作总结
2015/07/22 职场文书
Python爬虫爬取全球疫情数据并存储到mysql数据库的步骤
2021/03/29 Python