如何使用 Python 请求模块下载视频?
How to download video using Python requests module?
我正在尝试使用 python 请求模块下载显示在本网站底部的视频。
我可以找到视频 URL。但是,当我尝试在页面外使用它时,它给出了 404 状态代码。
有人可以帮助如何从网站上抓取视频吗?
提前致谢
要使用 requests
下载 mp4
视频,请设置 Referer
HTTP header:
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
并保存视频。
我正在尝试使用 python 请求模块下载显示在本网站底部的视频。 我可以找到视频 URL。但是,当我尝试在页面外使用它时,它给出了 404 状态代码。
有人可以帮助如何从网站上抓取视频吗?
提前致谢
要使用 requests
下载 mp4
视频,请设置 Referer
HTTP header:
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
并保存视频。