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

2024-10-01 15:46:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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)

有人能帮我吗


Tags: 标记comurl密码get视频responsehtml
1条回答
网友
1楼 · 发布于 2024-10-01 15:46:52

此脚本下载视频并将其保存为video.mp4。有必要指定User-AgentHTTP头:

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)

相关问题 更多 >

    热门问题