requests中text和content的区别

# -*- coding: utf-8 -*-
__author__ = "nixinxin"
import re

img_url = "https://f11.baidu.com/it/u=266030047,2129015355&fm=76"


from PIL import Image
from io import StringIO
import requests

# content 返回字节型数据(bytes型的数据): b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x
result = requests.get(img_url).content
with open("test.jpg", 'wb') as f:
    f.write(result)
i = Image.open('test.jpg')
i.show()
i.close()

# text 返回Unicode型的数据,可能是乱码
result = requests.get(img_url).text
print(result[:100])

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

 

posted @ 2018-03-26 21:48  倪兴国  阅读(557)  评论(0编辑  收藏  举报