对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标准模块--ContextManager上下文管理器的具体用法
Nov 27 Python
Python配置虚拟环境图文步骤
May 20 Python
Python二进制文件读取并转换为浮点数详解
Jun 25 Python
Python自动化导出zabbix数据并发邮件脚本
Aug 16 Python
Python 实现取多维数组第n维的前几位
Nov 26 Python
Python中断多重循环的几种方式详解
Feb 10 Python
wxPython修改文本框颜色过程解析
Feb 14 Python
Python 解析pymysql模块操作数据库的方法
Feb 18 Python
python如何提升爬虫效率
Sep 27 Python
python中delattr删除对象方法的代码分析
Dec 15 Python
用python对excel进行操作(读,写,修改)
Dec 25 Python
Python使用sql语句对mysql数据库多条件模糊查询的思路详解
Apr 12 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
php读取文件内容的几种方法详解
2013/06/26 PHP
php session_decode函数用法讲解
2019/05/26 PHP
php使用redis的有序集合zset实现延迟队列应用示例
2020/02/20 PHP
php屏蔽错误及提示的方法
2020/05/10 PHP
Span元素的width属性无效果原因及解决方案
2010/01/15 Javascript
通过jquery还原含有rowspan、colspan的table的实现方法
2012/02/10 Javascript
对于this和$(this)的个人理解
2013/09/08 Javascript
jquery使用jquery.zclip插件复制对象的实例教程
2013/12/04 Javascript
jquery进行数组遍历如何跳出当前的each循环
2014/06/05 Javascript
JS实现让网页背景图片斜向移动的方法
2015/02/25 Javascript
jquery实现滑动特效代码
2015/08/10 Javascript
Prototype框架详解
2015/11/25 Javascript
Bootstrap布局组件应用实例讲解
2016/02/17 Javascript
浅谈JavaScript的计时器对象
2016/12/26 Javascript
JavaScript 中调用 Kotlin 方法实例详解
2017/06/09 Javascript
利用jquery和BootStrap实现动态滚动条效果
2018/12/03 jQuery
微信小程序Page中data数据操作和函数调用方法
2019/05/08 Javascript
selenium+java中用js来完成日期的修改
2019/10/31 Javascript
Element InfiniteScroll无限滚动的具体使用方法
2020/07/27 Javascript
微信小程序实现日历签到
2020/09/21 Javascript
wxpython 学习笔记 第一天
2009/02/09 Python
Python读写ini文件的方法
2015/05/28 Python
tf.truncated_normal与tf.random_normal的详细用法
2018/03/05 Python
Python中面向对象你应该知道的一下知识
2019/07/10 Python
Python 爬虫实现增加播客访问量的方法实现
2019/10/31 Python
导入tensorflow:ImportError: libcublas.so.9.0 报错
2020/01/06 Python
tensorflow 20:搭网络,导出模型,运行模型的实例
2020/05/26 Python
Fairyseason:为个人和批发商提供女装和配件
2017/03/01 全球购物
瑜伽服装品牌:露露柠檬(lululemon athletica)
2017/06/04 全球购物
国贸类专业毕业生的求职信分享
2013/12/08 职场文书
会计大学生职业生涯规划书范文
2014/01/13 职场文书
公休请假条
2014/04/11 职场文书
班干部演讲稿
2014/04/24 职场文书
人口与计划生育目标管理责任书
2014/07/29 职场文书
详解CSS玩转图片Base64编码
2021/05/25 HTML / CSS
使用Python获取字典键对应值的方法
2022/04/26 Python