|
发表于 2023-10-2 16:21:37
|
显示全部楼层
试了下 requests下载文件, 支持 进度条更新 和 断点续传.
参考文章, https://www.justdopython.com/2020/11/16/python-downloadFile/
- import os,reimport requestsimport tqdmdef 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)
复制代码 |
|