0
点赞
收藏
分享

微信扫一扫

python中requests包的text和content方法的区别

两者返回的编码格式不同。

text返回的是Unicode编码,一般是在网页的header中定义的编码形式。

而content返回的是byte类型,二进制数据。


也就是说,如果单单提取文本,那么用text就可以了。

如果是想要保存图片、文件等等,需要用到content

例如:


for num, img in enumerate(img_url):
img_response = requests.get(img)
image = img_response.content
image_path = './pictures/%s.png' % num
fp = open(image_path, 'wb')
fp.write(image)
fp.close()

举报

相关推荐

0 条评论