import requests
from bs4 import BeautifulSoup
# 目标网页地址
url = 'https://www.pep.com.cn/rjyx/rjkj/202110/t20211015_1970988.shtml'
# 使用requests获取网页内容
response = requests.get(url)
web_content = response.text
# 使用BeautifulSoup解析网页
soup = BeautifulSoup(web_content, 'html.parser')
# 根据网页结构查找视频标签,这里以<video>标签为例
video_tags = soup.find_all('video')
# 遍历所有找到的视频标签
for video in video_tags:
# 视频的URL
video_url = video.get('src')
print('Found video URL:', video_url)
# 指定要保存视频的本地路径
local_filename = 'E:/360Downloads/python_download_video_example.mp4'
# 发送GET请求
response = requests.get(video_url, stream=True)
# 检查请求是否成功
if response.status_code == 200:
# 打开本地文件用于写入
with open(local_filename, 'wb') as f:
# 按块写入视频数据,减少内存使用
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print('Video download success,and save file dir :', local_filename)
else:
print('Video download failed,status code:', response.status_code)
--------------------------------------------------------------
补充:
1. 需要分析URL中的内容,提供的示例只有一个唯一视频
2.如果本地没有 requests和BeautifulSoup库,需要pip install 下载后方可使用