我一直在尝试下载以下带有python代码的视频。运行代码时,它总是下载一个空文件,但是,如果我在Web浏览器(Chrome)中打开URL,它将正确下载文件。该代码的响应为
标题:
{'Server': 'nginx', 'Date': 'Sat, 18 May 2019 17:31:02 GMT', 'Content-Type': 'application/download', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=20',
'Content-Disposition': 'attachment; filename=video0.mp4', 'Content-Description': 'File Transfer',
'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'X-Clacks-Overhead': 'GNU Terry Pratchett', 'X-HTTPS-Protocol': 'TLSv1.2',
'X-HTTPS-Cipher': 'ECDHE-ECDSA-AES256-GCM-SHA384'}
我从响应中获得的内容:
b''
我尝试使用的代码是
def downloadVideo(filename, url):
response = requests.get(url, stream=True)
open(filename, 'wb').write(response.content)
print ("Downloaded " + filename)
还有其他方法可以使这项工作吗?预先谢谢你
答案 0 :(得分:1)
如果它是一个大文件,则应分块编写,请尝试以下操作:
def downloadVideo(filename, url):
with requests.get(url, stream=True) as r:
with open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
print ("Downloaded " + filename)