Skip to main content
 首页 » 编程设计

python之在使用 python 请求下载之前获取 mp4 文件大小

2025年05月04日12lori

我正在编写小脚本来为我的 friend 从一些网站下载视频文件,我想为此添加进度条,但问题是标题中没有“内容长度”。我知道了。

size = requests.head(link["link"], headers={'Accept-Encoding': 'identity'}) 

我得到了这个作为返回:

'X-Varnish': 'HV116W137', 
'Age': '84', 
'Vary': 'Accept-Encoding', 
'Server': 'Apache', 
'Connection': 'keep-alive',  
'Via': '1.1 varnish',  
'cache-control': 'max-age=60, public,  
's-maxage=300',  
'Date': 'Tue, 22 Dec 2015 17:31:05 GMT',  
'x-frame-options': 'deny',  
'Content-Type': 'text/html; charset=UTF-8' 

链接:http://n-4-15.dcs.redcdn.pl/dcs/o2/tvn/web-content/m/p1/v/4e6cd95227cb0c280e99a195be5f6615/6398d4db-c842-49c7-8d1f-d1018cd0c95b-480p.mp4

我想这是可能的,因为浏览器可以在下载之前判断文件有多大。

请您参考如下方法:

size = requests.head(link['link']).headers['Content-Length']