Python 写一个下载网站内容的GUI工具,所有内容都能下载,真的太方便了!
- 前言
- 本次要实现的功能
- 效果展示
- 代码实战
- GUI部分
- 最后
前言
哈喽大家好,我是轻松。
今天我们分享一个用Python写下载视频+弹幕+评论的代码。
之前自游写了采集视频、弹幕、评论的代码,还录了视频。
我当时就问他,你就不能把这些写成GUI,把这些功能放到一起不是别人用起来更方便么?
本来他还想反抗,当时我就直接叫他看着办!
他哪受得了这种委屈,当时就乖乖写代码去了,现在我把代码分享给大家。
本次要实现的功能
咱们本次先简单的实现一下
效果展示
我们来看看实现效果吧
代码实战
主要代码分为界面和采集部分
获取数据
网址我屏蔽了,防止误杀。
获取视频
import requests
import re
import json
from pprint import pprint
import subprocess
import osdef 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).contentvideo_content = requests.get(url=video_url, headers=headers).contentif 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 osdef 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_encodingreturn responsedef get_Dm_url(bv_id):link = f'https://www.***.com/video/{bv_id}/'html_data = get_response(link).textDm_url = re.findall('弹幕', html_data)[0]title = re.findall(', html_data)[-1]return Dm_url, titledef get_Dm_content(Dm_url, title):html_data = get_response(Dm_url).textcontent_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 osdef 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 responsedef get_oid(bv_id):link = f'https://www.***.com/video/{bv_id}/'html_data = get_response(link).textoid = re.findall('window.__INITIAL_STATE__={"aid":(\d+),', html_data)[0]title = re.findall('"title":"(.*?)","pubdate"', html_data)[0].replace(' ', '')return oid, titledef get_content(oid, page, title):content_url = 'https://***.com/x/v2/reply/main'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
GUI部分
模块
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')
root.attributes("-alpha", 0.9)
tk.Label(root, text='完整源码领取+扣裙708525271', font=('黑体', 13), fg="red").grid(row=0, column=1)
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'] = ('视频', '弹幕', '评论')
numberChosen.grid(row=1, column=1, padx=5, pady=5)
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可执行文件,这样就能直接把软件分享给小伙伴一起用了。
或者直接找我要也可以。
大家觉得有用的话可以来个免费的点赞+收藏+关注,防止下次我悄悄更新了好东西却不知道!