爬取网站图片,新浪网的一个图片。可以存储到本地。
遇到问题: 第一就是没有python的基础,不知道缩进的意义,已经修改。
import requests
import os
url = "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
# img_R = requests.get(url)
root = "D:\\Users\\"
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)
print(r.status_code)
with open(path, 'wb') as f:
f.write(r.content)
f.close()
print("文件保存成功")
else:
print("文件已经存在")
except:
print("爬取失败")