我已经查看了urllib模块上的文档,但是,在尝试下载文件时,我不太了解如何向请求中添加标头。
我习惯于使用请求模块,但是,这对我来说不是一个可行的解决方案,因为它只是不下载任何内容。
Headers = {'Connection': 'keep-alive'}
index_name = 'Index.m3u8'
Videoindex = 'http://test.com/notreal.ts'
indexresponse = urllib.request.urlopen(Videoindex, headers=Headers)
with open(index_name,'wb') as x:
x.write(indexresponse.read())
audio_name = 'Audio.m3u8'
Audioindex = 'http://test.com/notreal.aac'
audioresponse = urllib.request.urlopen(Audioindex, headers=Headers)
with open(audio_name,'wb') as y:
y.write(audioresponse.read())
我了解到您无法解析urlopen中的标头参数,但是有什么方法可以添加此参数?我宁愿不逐行实现标头,因为有很多标头。
感谢您的帮助
答案 0 :(得分:2)
对于这种类型的工作,您确实应该使用requests
。它使生活更轻松。
https://requests.readthedocs.io/en/master/user/quickstart/#custom-headers
答案 1 :(得分:0)
我第二次使用请求库方法。它允许rest api功能。您还可以指定查询参数。
Videoindex = 'http://test.com/notreal.ts'
Headers = {'Connection': 'keep-alive'}
r = requests.get(url=VideoIndex, headers=Headers)