import requests
import os
root = 'C://Users//HP123//Desktop//new//z//'
url = "http://upload-images.jianshu.io/upload_images/3459828-fe024299e76685da.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"
kv = {'user-agent':'Mozilla/5.0'}
path = root+url.split('/')[-1][1:5]+'.png'
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r = requests.get(url,headers = kv)
r.raise_for_status()
with open(path,'wb') as f:
f.write(r.content)
f.close()
print('done')
else:
print('exited')
except:
print("failed")
可能出现的问题:
爬是爬下来了,但是网上有些图片打不开,重新换张图片地址,尽量找那些他们放在文本的图片试试,百度出来的图片有些打不开……