python requests text content_python requests的content和text方法的区别

requests模块的content和text属性的区别,从print 结果来看是没有任何区别的。从源码描述信息上来看,可以发现resp.text返回的是Unicode型的数据、resp.content返回的是bytes型也就是二进制的数据。也就是说,如果你想取文本,可以通过r.text。如果想取图片,文件,则可以通过r.content。如果要取回json数据,可以使用resp.json()返回json格式。

content和text方法的描述信息

@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."""

测试示例

import requests

jpg_url = 'http://wiki.361way.com/static/logo.png'

content = requests.get(jpg_url).content

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

fp.write(content)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值