前言
嗨喽~大家好呀,这里是小曼呐 ❤ ~!
天我们分享一个用Python写下载视频+弹幕+评论的代码。
顺便把这些写成GUI,把这些功能放到一起让朋友用起来更方便~
主要代码分为界面和采集部分
网址我屏蔽了,防止误杀。
import requests
import re
import json
from pprint import pprint
import subprocess
import os
def Video(bv_id):
url = f'https://www.***.com/video/{bv_id}'
headers = {
# 防盗链
'referer': 'https://www.***.com/video/',
# 浏览器基本身份标识 表示浏览器
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
发送请求 —>
response = requests.get(url=url, headers=headers)
获取视频标题
title = re.findall('"title":"(.*?)","pubdate"', response.text)[0].replace(' ', '')
获取视频数据信息 前端标签两个两个一起
html_data = re.findall('', response.text)[0]
转换数据类型 字符串数据转成json字典数据类型
json_data = json.loads(html_data)
# print打印字典数据, 输出一行内容 print(json_data)
# pprint 打印字典数据, 格式化输出 展开效果 pprint(json_data)
# 字典数据 B站数据 音频和视频分开的 根据冒号左边的内容, 提取冒号右边的内容 键值对取值
完整源码、解答、教程皆+VX:Python10010验证码“M”获取
audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
video_url = json_data['data']['dash']['video'][0]['baseUrl']
# 403 Forbidden 没有访问权限.....
audio_content = requests.get(url=audio_url, headers=headers).content
video_content = requests.get(url=video_url, headers=headers).content
if not os.path.exists('video\\'):
os.mkdir('video\\')
with open('video\\' + title + '.mp3', mode='wb') as audio:
audio.write(audio_content)
with open('video\\' + title + '.mp4', mode='wb') as video:
video.write(video_content)
# 获取音频内容以及视频画面内容
cmd = f"ffmpeg -i video\\{title}.mp4 -i video\\{title}.mp3 -c:v copy -c:a aac -strict experimental video\\{title}output.mp4"
subprocess.run(cmd, shell=True)
os.remove(f'video\\{title}.mp4')
os.remove(f'video\\{title}.mp3')
return title
import requests
import re
import os
def get_response(html_url):
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
response = requests.get(url=html_url, headers=headers)
response.encoding = response.apparent_encoding
return response
def get_Dm_url(bv_id):
link = f'https://www.***.com/video/{bv_id}/'
html_data = get_response(link).text
Dm_url = re.findall('弹幕', html_data)[0]
title = re.findall(', html_data)[-1]
return Dm_url, title
def get_Dm_content(Dm_url, title):
html_data = get_response(Dm_url).text
content_list = re.findall('(.*?) ', html_data)
if not os.path.exists('弹幕\\'):
os.mkdir('弹幕\\')
for content in content_list:
with open(f'弹幕\\{title}弹幕.txt', mode='a', encoding='utf-8') as f:
f.write(content)
f.write('\n')
def main(bv_id):
Dm_url, title = get_Dm_url(bv_id)
get_Dm_content(Dm_url, title)
import requests
import re
import os
def get_response(html_url, params=None):
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
response = requests.get(url=html_url, params=params, headers=headers)
return response
def get_oid(bv_id):
link = f'https://www.***.com/video/{bv_id}/'
html_data = get_response(link).text
oid = re.findall('window.__INITIAL_STATE__={"aid":(\d+),', html_data)[0]
title = re.findall('"title":"(.*?)","pubdate"', html_data)[0].replace(' ', '')
return oid, title
def get_content(oid, page, title):
content_url = 'https://***.com/x/v2/reply/main'
# 完整源码、解答、教程皆+VX:oudashuai1(加好友验证码填“M”)
data = {
'csrf': '6b0592355acbe9296460eab0c0a0b976',
'mode': '3',
'next': page,
'oid': oid,
'plat': '1',
'type': '1',
}
json_data = get_response(content_url, data).json()
content = '\n'.join([i['content']['message'] for i in json_data['data']['replies']])
if not os.path.exists('评论\\'):
os.mkdir('评论\\')
with open(f'评论\\{title}评论.txt', mode='a', encoding='utf-8') as f:
f.write(content)
def main(bv_id):
oid, title = get_oid(bv_id)
for page in range(1, 6):
try:
get_content(oid, page, title)
except:
pass
模块
import tkinter as tk
from tkinter import ttk
import tkinter.messagebox
from Video import Video
import Barrage
import Comment
下载完成提示
def get_content():
result = number_int_var.get()
if result == '视频':
bv_id = bv_va.get()
title = Video(bv_id)
tk.messagebox.showinfo(title='温馨提示', message=f'{title}下载完成')
elif result == '弹幕':
bv_id = bv_va.get()
Barrage.main(bv_id)
tk.messagebox.showinfo(title='温馨提示', message=f'弹幕下载完成')
elif result == '评论':
bv_id = bv_va.get()
Comment.main(bv_id)
tk.messagebox.showinfo(title='温馨提示', message=f'评论下载完成')
主界面部分
root = tk.Tk()
root.title('B站视频下载软件')
root.geometry('367x134+200+200')
# 透明度的值:0~1 也可以是小数点,0:全透明;1:全不透明
root.attributes("-alpha", 0.9)
# -------------------------------------------------------
tk.Label(root, text='完整源码、解答、教程皆+VX:oudashuai1(加好友验证码填“M”), font=('黑体', 13), fg="red").grid(row=0, column=1)
# 我已经把这个工具打包成了exe可执行文件,
# -------------------------------------------------------
text_label_1 = tk.Label(root, text='选择: ', font=('黑体', 15))
text_label_1.grid(row=1, column=0, padx=5, pady=5)
# -------------------------------------------------------
number_int_var = tk.StringVar()
# 创建一个下拉列表
numberChosen = ttk.Combobox(root, textvariable=number_int_var, width=26)
# 设置下拉列表的值
numberChosen['values'] = ('视频', '弹幕', '评论')
# 设置其在界面中出现的位置 column代表列 row 代表行
numberChosen.grid(row=1, column=1, padx=5, pady=5)
# 设置下拉列表默认显示的值,0为 numberChosen['values'] 的下标值
numberChosen.current(0)
# -------------------------------------------------------
text_label = tk.Label(root, text='BV号:', font=('黑体', 15))
text_label.grid(row=2, column=0, padx=5, pady=5)
bv_va = tk.Variable()
entry_1 = tk.Entry(root, font=('黑体', 15), textvariable=bv_va)
entry_1.grid(row=2, column=1)
Button_1 = tk.Button(root, text='下载', font=('黑体', 13), command=get_content)
Button_1.grid(row=2, column=2, padx=5, pady=5)
# -------------------------------------------------------
root.mainloop()
尾语
最后感谢你观看我的文章呐~本次航班到这里就结束啦
希望本篇文章有对你带来帮助 ,有学习到一点知识~