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)