在补充了 with open用法以及 os 的使用之后,完成了以下网络图片爬取与存储的内容
import requests
import os
url = "https://images.youkuaiyun.com/20171113/timg.png"
root = "F://"
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: #write binary -》wb
f.write(r.content)
print("文件保存成功")
else:
print("文件已存在")
except:
print("爬取失败")