import requests
import re
import json
import subprocess
import time
# 设置请求头信息
headers = {
'User-Agent': '',
"referer": "https://www.bilibili.com",
}
# 视频链接列表
video_urls = [
"https://www.bilibili.com/video/BV1hT411R7PJ?p=4",
]
for url in video_urls:
response = requests.get(url=url, headers=headers)
print("返回200,则网页请求成功:", response)
# 提取视频标题
title = re.findall('<h1 title="(.*?)"', response.text)[0]
title = re.sub(r"[\/:*?<>|]", "", title)
print("视频标题为:", title)
# 提取视频信息
html_data = re.findall('<script>window.__playinfo__=(.*?)</script>', response.text)[0]
json_data = json.loads(html_data)
json_dicts = json.dumps(json_data, indent=4)
# 打印视频分辨率和选用的视频流质量
for video_stream in json_data["data"]["dash"]["video"]:
print("视频分辨率:{}x{}, 视频流质量:{}".format(video_stream["width"], video_stream["height"], video_stream["id"]))
# 选择最高质量的视频流
selected_video_stream = max(json_data["data"]["dash"]["video"], key=lambda x: x["width"])
# 下载视频
video_url = selected_video_stream["baseUrl"]
video_content = requests.get(url=video_url, headers=headers).content
with open(f"{title}.mp4", mode="wb") as f:
f.write(video_content)
time.sleep(4)
# import requests,re,json,subprocess,time
# from bs4 import BeautifulSoup
# audio_url = json_data["data"]["dash"]["audio"][0]["baseUrl"]
# print("音频地址为:", audio_url)
# video_content = requests.get(url=video_url,headers=headers).content
# audio_content = requests.get(url=audio_url,headers=headers).content
# name=url.replace("https://www.bilibili.com/video/BV1954y1w7yZ?p=","")
# with open (f"第{name}局"+".mp4", mode = "wb") as f :
# f.write(video_content)
# time.sleep(4)
# with open (title+".mp3", mode = "wb") as f :
# f.write(audio_content)
# cmd =f"ffmpeg -i {title}.mp4 -i {title}.mp3 -c:v copy -c:a aac -strict experimental {title}(最终版).mp4"
# subprocess.run(cmd,shell=True)
爬取bilibili视频
最新推荐文章于 2024-08-24 11:00:49 发布