import osimport request def DownloadFile(url, savePath): if not os.path.exists(savePath): print("downloading {0}".format(url)) kv={‘user-agent‘:‘Mozilla/5.0‘} try: r = requests.get(url,headers=kv, timeout=60) r.raise_for_status() # 如果响应状态码不是 200,就主动抛出异常 with open(savePath,‘wb‘)as f: f.write(r.content) f.close() print("save file succeed") time.sleep(1) except requests.RequestException as e: print(e) with open(‘D:/timeout.txt‘, ‘a+‘,encoding = ‘UTF-8‘) as fw: #读入存储文件路径 fw.write(‘%s,%s\n‘ % (url,e)) else: print("file already exists")#
一个简单的下载,并指定保存位置的函数
原文地址:https://www.cnblogs.com/yanghao2008/p/10499018.html
时间: 2024-10-08 08:10:48