import requests
import os
url = “(所需爬取的图片地址)”
root = “C://pices//”#存储图片的文件
path = root + url.split(’/’)[-1]#选取图片url最后一段以/分割的字符
try:
if not os.path.exists(root):#判别保存的目录是否存在
os.mkdir(root)#不存在则创造文件夹
if not os.path.exists(path):
r = requests.get(url)
with open(path, ‘wb’) as f:
f.write(r.content)#把获取的图片文件写入
f.close()
print(“文件保存成功”)
else:
print(“文件已存在”)
except:
print(‘爬取失败’)
python简单的图片爬取
最新推荐文章于 2023-06-12 13:49:34 发布