【问题标题】: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)