用python爬取某一个图片并保存到本地

···
import requests
import os
url = "http://g.hiphotos.baidu.com/space/pic/item/8cb1cb13495409237b0badc39258d109b3de4929.jpg"
root = "D://pics//"
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("爬取失败")
···

你可能感兴趣的:(用python爬取某一个图片并保存到本地)