- 注册时间
- 2009-2-12
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 22688
- 在线时间
- 小时
|
发表于 2021-12-3 21:09:28
|
显示全部楼层
试了下 requests下载文件, 支持 进度条更新 和 断点续传.
参考文章, https://www.justdopython.com/2020/11/16/python-downloadFile/
- import os,re
- import requests
- import tqdm
- def download_file(url, file):
- resp = requests.get(url+file, stream=True)
- file_size = int(resp.headers['content-length'])
- print('{0} ,filesize: = {1:.3f} MB'.format(file, file_size/1024./1024.))
- with requests.get(url+file, stream=True) as r:
- with open(file, 'wb') as fp:
- for chunk in r.iter_content(chunk_size=1024):
- if chunk:
- fp.write(chunk)
- def tqdm_download(url, file_name):
- r = requests.get(url + file_name, stream=True)
- # 获取文件大小
- file_size = int(r.headers['content-length'])
- # 如果文件存在获取文件大小,否在从 0 开始下载,
- first_byte = 0
- if os.path.exists(file_name):
- first_byte = os.path.getsize(file_name)
- # 判断是否已经下载完成
- if first_byte >= file_size:
- return
- # Range 加入请求头
- header = {"Range": f"bytes={first_byte}-{file_size}",
- 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.3538.77 Safari/537.36'}
- # 加了一个 initial 参数
- with tqdm.tqdm(total=file_size, unit='B', initial=first_byte, unit_scale=True, unit_divisor=1024, ascii=True, desc=file_name) as bar:
- # 加 headers 参数
- with requests.get(url + file_name, headers=header, stream=True) as r:
- with open(file_name, 'ab') as fp:
- for chunk in r.iter_content(chunk_size=512):
- if chunk:
- fp.write(chunk)
- bar.update(len(chunk))
- u='https://mirrors.cloud.tencent.com/qt/official_releases/qt/6.2/6.2.2/submodules/'
- r = requests.get(u)
- res = re.findall(r'([\w+\-\.]+xz)</a>', r.text)
- for file in res:
- # download_file(u, file)
- tqdm_download(u, file)
复制代码 |
|