网络图片的爬取和存储
1 """网络图片的爬取和存储""" 2 3 4 import requests 5 import os 6 7 url = "http://image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg" 8 root = "D://pics//" 9 path = root + url.split('/')[-1] 10 # D://pics//20170211061910157.jpg 11 # print(path) 12 try: 13 if not os.path.exists(root): 14 os.mkdir(root) 15 if not os.path.exists(path): 16 r = requests.get(url) 17 with open(path, 'wb') as f: 18 f.write(r.content) 19 f.close() 20 print("文件保存成功") 21 else: 22 print("文件已存在") 23 except: 24 print("爬取失败")