如何在python中下载以CDN开头的视频网址

问题描述:

我正在尝试从此 URL 下载视频://cdn.muscleandstrength.com/video/reversegripbentoverdumbbellrow.mp4

I am trying to download a video from this URL: //cdn.muscleandstrength.com/video/reversegripbentoverdumbbellrow.mp4

但是当我处理请求时,我只得到一个 HTML 标记.这是我的代码:

But when I do it with requests I am only getting an HTML markup. Here is my code:

response = requests.get("https://www.muscleandstrength.com/video/highinvertedrow.mp4", allow_redirects=True)
with open("data/video.mp4", 'wb') as file:
    file.write(response.content)

有人可以帮我吗?

此脚本下载视频并将其保存为 video.mp4.需要指定User-Agent HTTP 头:

This script downloads the video and saves it as video.mp4. It's necessary to specify User-Agent HTTP header:

import requests


url = 'https://cdn.muscleandstrength.com/video/reversegripbentoverdumbbellrow.mp4'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
    
with open('video.mp4', 'wb') as f_out:
    r = requests.get(url, headers=headers, stream=True)
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:
            f_out.write(chunk)