公司项目需要下载一批视频文件, 格式是mp4和mkv的,就借助request模块进行了下载,前提是源服务器返回文件的大小,以及可以接受 请求头headers中带有Range参数
以下是下载逻辑:
resp = requests.head(url=real_video_url)
headers = {}
try:
totalfilesize = int(resp.headers['Content-Length'])
except Exception as e:
print(e.args)
if not os.path.exists(real_video_download_path):
with open(real_video_download_path, 'ab+') as f:
pass
fsize1 = os.path.getsize(real_video_download_path)
flag = True
while True:
if flag:
try:
fsize1 = os.path.getsize(real_video_download_path)
headers['Range'] = 'bytes=%d-' % fsize1
resp = requests.get(url=real_video_url, headers=headers, stream=True, timeout=20)
with open(real_video_download_path, 'ab+') as f:
for chunk in resp.iter_content(chunk_size=4096):
if resp.status_code == 206:
if chunk:
f.write(chunk)
elif os.path.getsize(real_video_download_path) >= totalfilesize:
# 出现这种情况,就说明已经下载完毕
flag = False
return real_video_download_path
else:
# 出现异常
time.sleep(3)
break
except Exception as e:
print(e.args)
else:
break