网络图片链接的格式:
http://www.example.com/picture.jpg
http://img0.dili360.com/ga/M00/34/5E/wKgBzFS8cISAYYg5AB5r1Gar-oQ103.tub.jpg
1 import requests 2 import os 3 root='D://abc//' 4 url='http://img0.dili360.com/ga/M00/34/5E/wKgBzFS8cISAYYg5AB5r1Gar-oQ103.tub.jpg' 5 path=root+url.split('/')[-1] 6 try: 7 if not os.path.exists(root):#查看给定的文件/目录是否存在,存在返回True,不存在返回False。 8 os.mkdir(root) 9 if not os.path.exists(path): 10 r=requests.get(url) 11 with open(path,'wb') as f: 12 f.write(r.content) 13 f.close() 14 print('保存成功') 15 else: 16 print('文件已存在') 17 except: 18 print('爬取失败')
标签:网页,root,http,jpg,爬取,print,path,os,图片 From: https://www.cnblogs.com/ddpjj/p/17152736.html