data:image/jpg;base64格式数据转化为图片

在做爬虫爬取图片时,发现有的图片url是用“data:image/jpg;base64” 开头的,例如下图
在这里插入图片描述
此时获取url之后需要对url进行base64解密,解密之后的数据即可写入图片,详见下方代码

img_imf = img_imf.replace('data:image/jpg;base64,','')

#将data:image/jpg;base64格式的数据转化为图片
page_content = base64.b64decode(img_imf)
file_path = './code.jpg'
with open(file_path, 'wb') as f:
	f.write(page_content)

猜你喜欢

转载自blog.csdn.net/zhuan_long/article/details/126143998