import os
import requests

def
DownloadFile(url, savePath):
#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")#

 

一个简单的下载,并指定保存位置的函数。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-24
  • 2021-08-21
  • 2021-12-27
  • 2021-07-04
  • 2021-12-17
  • 2021-12-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-14
  • 2022-01-05
相关资源
相似解决方案