import requests
path='xiaozhan.jpg'
url='https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D500/sign=492a71febd1bb0518b24b328067bda77/58ee3d6d55fbb2fba34b25ce414a20a44723dc8e.jpg'
r=requests.get(url)
print(r.status_code)
with open(path,'wb') as f:
f.write(r.content)
f.close()
图片爬取全代码:
import requests
import os
url='https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D500/sign=492a71febd1bb0518b24b328067bda77/58ee3d6d55fbb2fba34b25ce414a20a44723dc8e.jpg'
root='F://code//python//reptile//'
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('爬取失败')