【问题标题】:How to download video using Python requests module? [closed]如何使用 Python 请求模块下载视频? [关闭]
【发布时间】:2021-10-22 19:35:55
【问题描述】:

我正在尝试使用 python 请求模块下载显示在本网站底部的视频。 我可以找到视频网址。但是,当我尝试在页面外使用它时,它会给出 404 状态代码。

有人可以帮助如何抓取网站上的视频吗?

提前致谢

【问题讨论】:

  • 显示在哪个网站的底部?是什么阻止你自己刮掉它?请阅读How to Ask

标签: python html web-scraping get


【解决方案1】:

要下载带有requestsmp4 视频,请设置Referer HTTP 标头:

import requests
from bs4 import BeautifulSoup

url = "https://drswamyplabvideo.com"
headers = {"Referer": "https://drswamyplabvideo.com/"}

soup = BeautifulSoup(requests.get(url).content, "html.parser")
for v in soup.select("video source[src]"):
    print("Downloading {}".format(v["src"]))
    with open(v["src"].split("/")[-1].strip(), "wb") as f_out:
        f_out.write(requests.get(v["src"].strip(), headers=headers).content)

打印:

Downloading https://drswamyplabvideo.com/videos/Demo video.mp4
Downloading https://drswamyplabvideo.com/videos/CNS Day 1 by Nosheen Part 1.mp4
Downloading https://drswamyplabvideo.com/videos/CNS Part 5.mp4 
Downloading https://drswamyplabvideo.com/videos/Lady with fracture wrist Talk to son.mp4

并保存视频。

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 2012-02-09
    • 2017-10-11
    • 2017-06-15
    • 2015-09-18
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多