requests库爬取图片

import requests
import os
url = ‘’
root = ‘D:/aa.jpg’
path = root + url.split(’/’)[-1]
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(‘失败’)

你可能感兴趣的:(requests库爬取图片)