python requests的content和text方法的区别(转)
2017-12-28 14:51
435 查看
原文地址: http://blog.csdn.net/xie_0723/article/details/51361006
问题:
结论是:
也就是说,如果你想取文本,可以通过r.text。
如果想取图片,文件,则可以通过r.content。
(resp.json()返回的是json格式数据)
举个栗子
问题:
一直在想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 requests的content和text方法的区别
- python requests的content和text方法的区别
- python_requests的content和text方法的区别
- requests入门 response的常用方法 response.text 和response.content的区别
- python requests 详细学习content 和text 的区别
- python3 requests的content和text方法
- Python的requestcontent和text的区别
- python requests的content和text方法的区别(转)
- requests的content和text的区别
- text和content方法的区别
- python3中request.urlopen()和requests.get()方法的区别
- python中requests库content和text的区别
- python response.text和response.content的区别
- python中response.text与response.content的区别
- python中set和frozenset方法和区别
- Supporting Python 3(支持python3)——语言区别和暂时解决方法
- <%@page contentType="text/html;charset=GBK"%>与<meta content="text/html; charset=GBK">区别
- TextUtils.isEmpty()和equals()方法的用法区别
- Python的file()和open()方法的区别