嗨喽,大家好呀~这里是爱看美女的茜茜呐
今天我们来分享一个用Python一键下载视+弹幕+评论的代码。
之前写了采集视、弹幕、评论的代码,还录了视频。
后面就有人说,你就不能把这些写成GUI,把这些功能放到一起用起来更方便么?
我一想,也是噢~所以这篇文章,当当当!!的就诞生了
评论
弹幕
视
Python 3.8 / 编译器
Pycharm 2021.2版本 / 编辑器
第三方模块安装:
在cmd里面或者pycharm终端里面进行安装 pip install requests 安装。
获取视
导入模块
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_data = json.loads(html_data)
audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
video_url = json_data['data']['dash']['video'][0]['baseUrl']
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, 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'
data = {
'csrf': '6b0592355acbe9296460eab0c0a0b976',
}
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 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'
data = {
'csrf': '6b0592355acbe9296460eab0c0a0b976',
}
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('视下载软件')
root.geometry('367x134+200+200')
# 透明度的值:0~1 也可以是小数点,0:全透明;1:全不透明
root.attributes("-alpha", 0.9)
# -------------------------------------------------------
tk.Label(root, text='完整源码领取+我V:qian97378', font=('黑体', 13), fg="red").grid(row=0, column=1)
# 我已经把这个工具打包成了exe可执行文件,直接加我V获取。
# -------------------------------------------------------
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()
像评论、弹幕咱们获取到以后,还能做成词云图等等,
视下载下来有水印,也能用Python直接去除视水印,非常方便。
大家还可以把代码打包成exe可执行文件,这样就能直接把软件分享给小伙伴一起用了。
或者直接找我要也可以。
大家觉得有用的话可以来个免费的点赞+收藏+关注,
防止下次我悄悄更新了好东西却不知道!
感谢你观看我的文章呐~本次航班到这里就结束啦
希望本篇文章有对你带来帮助 ,有学习到一点知识~
躲起来的星星也在努力发光,你也要努力加油(让我们一起努力叭)。
最后,宣传一下呀~更多源码、资料、素材、解答、交流皆点击下方名片获取呀