python断点续传
2023-04-06 本文已影响0人
是东东
通过设置headers['range'] 字段来告诉程序从哪里开始下载,即断点续传
下载块 除以 文件总大小 即为 下载百分百
下载块 除以 时间差值 即为 当前网速
# coding: utf-8
import re
import requests
from pathlib import Path
from time import time, perf_counter
def download_file_from_url(dl_url, file_name, headers):
file_path = Path(__file__).parent.joinpath(file_name)
if file_path.exists():
dl_size = file_path.stat().st_size
else:
dl_size = 0
headers['range'] = f'bytes={dl_size}-'
response = requests.get(dl_url, stream=True, headers=headers)
print('\n\n' + '*' * 30 + '下载信息' + '*' * 30)
total_size = int(response.headers['content-length'])
print(
f'\n\n文件名称:{file_name}\t\t已下载文件大小:{dl_size / 1024 / 1024:.2f}M\t\t文件总大小:{total_size / 1024 / 1024:.2f}M\n\n')
start = perf_counter()
data_count = 0
count_tmp = 0
start_time = time()
with open(file_path, 'ab') as fp:
for chunk in response.iter_content(chunk_size=512):
data_count += len(chunk)
now_pross = (data_count / total_size) * 100
mid_time = time()
if mid_time - start_time > 0.1:
speed = (data_count - count_tmp) / 1024 / (mid_time - start_time)
start_time = mid_time
count_tmp = data_count
print(
f"\rDownloading.........{now_pross:.2f}%\t{data_count // 1024}Kb/{total_size // 1024}Kb\t当前下载速度:{speed:.2f}Kb/s",
end='')
fp.write(chunk)
end = perf_counter()
diff = end - start
speed = total_size / 1024 / diff
print(
f'\n\n下载完成!耗时:{diff:.2f}秒, 平均下载速度:{speed:.2f}Kb/s!\n文件路径:{file_path}\n')
if __name__ == '__main__':
url = 'https://www.douyin.com/aweme/v1/play/?video_id=v0200fg10000cglbh1rc77ubb5oatm5g&line=0&file_id=224c240378ae47d49b60edd91ca34ee0&sign=8ead581c35b9dc73cefb878d07a2ab61&is_play_url=1&source=PackSourceEnum_FEED&aid=6383'
# filename = url.rpartition('/')[-1]
filename = "test.mp4"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36', }
download_file_from_url(url, filename, headers)
未解决:下载进度没有将已经下载好的没有计算在内