Requests Response

1.response.text自动解码服务器返回的内容

import requests
r = requests.get('https://api.github.com/events')
r.text # u'[{"repository":{"open_issues":0,"url":"https://github.com/...

默认使用r.encoding=utf8,可以更改为r.encoding = 'ISO-8859-1',更改后,每次访问.text就直接使用该编码

2.response.content获得字节.text就是根据.content解码出来的

import requests
r = requests.get('https://api.github.com/events')
r.content # b'[{"repository":{"open_issues":0,"url":"https://github.com/...
from PIL import Image
from io import BytesIO
i = Image.open(BytesIO(r.content))

3.json

import requests
r = requests.get('https://api.github.com/events')
r.json()

3.response.raw获得字节(和content相似,应该是将ascii码也用十六进制表示的意思),原始套接字,要打开stream参数

r = requests.get('https://api.github.com/events', stream=True)
r.raw
r.raw.read(10)

一般使用以下形式保存

with open(filename, 'wb') as fd:
    for chunk in r.iter_content(chunk_size=128):
        fd.write(chunk)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值