【问题标题】:How to download a video url that starts with CDN in python如何在python中下载以CDN开头的视频url
【发布时间】:2020-07-19 11:13:09
【问题描述】:

我正在尝试从此 URL 下载视频: //cdn.muscleandstrength.com/video/reversegripbentoverdumbbellrow.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)

谁能帮帮我?

【问题讨论】:

标签: python web-scraping python-requests


【解决方案1】:

此脚本下载视频并将其保存为video.mp4。需要指定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)

【讨论】:

    猜你喜欢
    • 2018-06-10
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    相关资源
    最近更新 更多