0
点赞
收藏
分享

微信扫一扫

爬虫-requests模块保存图片及(content和text的区别)


这里找一张图片地址:
百度的图片地址:

https://www.baidu.com/img/baidu_jgylogo3.gif

使用requests模块的get方法访问地址

#encoding:utf-8
import requests
response=requests.get("https://www.baidu.com/img/baidu_jgylogo3.gif")
#保存
with open("b.gif","wb") as f: #保存的文件名 保存的方式(wb 二进制 w 字符串)
f.write(response.content)

同理 视频 .MP4 .flv 等其他类型的数据都可以用二进制文件进行保存

response.content 为获取返回的字节类型内容

response.text 为用默认的解码方式获取str类型的返回内容

如果需要指定编码

可以使用:​​response.encoding="utf-8"​​ str 使用encode方法转化为 bytes

bytes通过decode转化为str

执行程序后可以看见图片已经保存在项目的目录下:

爬虫-requests模块保存图片及(content和text的区别)_文件名


举报

相关推荐

0 条评论