1 def save_file(): 2 url = ‘http://css.8684.cn/citys/images/line/45.jpg‘ 3 root = r‘d:/pics/‘ 4 path = root + url.split(‘/‘)[-1] 5 try: 6 if not os.path.exists(root): 7 os.mkdir(root) 8 if not os.path.exists(path): 9 r = requests.get(url) 10 with open(path, ‘wb‘) as f: 11 f.write(r.content) 12 f.close() 13 print("文件保存成功") 14 else: 15 print("文件已存在") 16 except: 17 print("爬取失败")