python3 requests的content和text方法

text返回的是Unicode型的数据
content返回的是是二进制的数据。
也就是说,如果你想取文本,可以通过r.text。
如果想取图片,文件,则可以通过r.content

>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.text
'{"message":"Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}'
>>> r.content
b'{"message":"Hello there, wayfaring stranger. If you\xe2\x80\x99re reading this then you probably didn\xe2\x80\x99t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}'

例子:

import requests
r = requests.get('http://img2.niutuku.com/1312/0804/0804-niutuku.com-27840.jpg')

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

参考:
http://cn.python-requests.org/zh_CN/latest/user/quickstart.html#id3
http://blog.csdn.net/xie_0723/article/details/51361006

转载于:https://www.cnblogs.com/fanren224/p/8457234.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值