- 来源:mooc
- 代码
import requests
import os
url = 'https://img14.360buyimg.com/n0/jfs/t1/61680/30/9004/177370/5d6e7867E60933ee2/96d75bd8a98945f4.jpg'
root = 'G://taobaopicture//' # 根目录
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 file:
file.write(r.content)
file.close()
print('文件保存成功')
else:
print('文件已存在')
except:
print('文件保存失败')
- 效果图