对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通过post提交数据的方法
May 06 Python
python3使用urllib模块制作网络爬虫
Apr 08 Python
使用Python读取大文件的方法
Feb 11 Python
Python3 jupyter notebook 服务器搭建过程
Nov 30 Python
python 输出所有大小写字母的方法
Jan 02 Python
selenium python 实现基本自动化测试的示例代码
Feb 25 Python
PyQt5显示GIF图片的方法
Jun 17 Python
numpy.linalg.eig() 计算矩阵特征向量方式
Nov 29 Python
Numpy之reshape()使用详解
Dec 26 Python
Python调用Windows API函数编写录音机和音乐播放器功能
Jan 05 Python
Selenium python时间控件输入问题解决方案
Jul 22 Python
Python绘制散乱的点构成的图的方法
Apr 21 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
re0第二季蕾姆被制作组打入冷宫!艾米莉亚女主扶正,原因唏嘘
2020/04/02 日漫
在PHP里得到前天和昨天的日期的代码
2007/08/16 PHP
单点登录 Ucenter示例分析
2013/10/29 PHP
两种设置php载入页面时编码的方法
2014/07/29 PHP
自己写的兼容低于PHP 5.5版本的array_column()函数
2014/10/24 PHP
thinkPHP显示不出验证码的原因与解决方法分析
2017/05/20 PHP
PHP copy函数使用案例代码解析
2020/09/01 PHP
extjs tabpanel限制选项卡数量实现思路及代码
2013/04/02 Javascript
B/S模式项目中常用的javascript汇总
2013/12/17 Javascript
javascript/jquery获取地址栏url参数的方法
2014/03/05 Javascript
node.js中的fs.createWriteStream方法使用说明
2014/12/17 Javascript
javascript实现捕捉键盘上按下的键
2015/05/05 Javascript
jQuery下拉框的简单应用
2016/06/24 Javascript
详解AngularJs ui-router 路由的简单介绍
2017/04/26 Javascript
xmlplus组件设计系列之路由(ViewStack)(7)
2017/05/02 Javascript
第一个Vue插件从封装到发布
2017/11/22 Javascript
sublime text配置node.js调试(图文教程)
2017/11/23 Javascript
详解vue项目的构建,打包,发布全过程
2017/11/23 Javascript
jQuery中的$是什么意思及 $. 和 $().的区别
2018/04/20 jQuery
elementUI Tree 树形控件的官方使用文档
2019/04/25 Javascript
vue实现的请求服务器端API接口示例
2019/05/25 Javascript
mpvue实现小程序签到金币掉落动画(api实现)
2019/10/17 Javascript
解决vue项目中遇到 Cannot find module ‘chalk‘ 报错的问题
2020/11/05 Javascript
[33:42]LGD vs OG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
Python抽象类的新写法
2015/06/18 Python
python好玩的项目—色情图片识别代码分享
2017/11/07 Python
快速查询Python文档方法分享
2017/12/27 Python
matplotlib作图添加表格实例代码
2018/01/23 Python
python函数不定长参数使用方法解析
2019/12/14 Python
pygame实现弹球游戏
2020/04/14 Python
pytorch加载自己的图像数据集实例
2020/07/07 Python
自然健康的概念:Natural Healthy Concepts
2020/01/26 全球购物
应届大学毕业生找工作的求职信范文
2013/11/29 职场文书
数学兴趣小组活动总结
2014/07/08 职场文书
Nginx已编译的nginx-添加新模块
2021/04/01 Servers
Python 读取千万级数据自动写入 MySQL 数据库
2022/06/28 Python