代码
`import requests
import os
kv={'user-agent':'Mozilla /5.0'}
url="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1857821645,3826618458&fm=23&gp=0.jpg"
root="G:"
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,headers=kv)
with open(path,'wb') as f:
f.write(r.content)
f.close()
print("文件保存成功")
else:
print("文件已存在")
except:
print("ERROR!!")`
这里用到requests库,同时在代码中将user-agent修改为用户标准,代码主体用到try except 结构。