使用python tkinter开发一个爬取B站直播弹幕工具的实现代码

作者:阿壮Jonsson 时间:2021-06-02 19:15:06 

项目地址

https://github.com/jonssonyan...

开发工具 python 3.7.9

pycharm 2019.3.5 代码


import threading
import time
import tkinter.simpledialog
from tkinter import END, simpledialog, messagebox

import requests

class Danmu():
def __init__(self, room_id):
 # 弹幕url
 self.url = 'https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory'
 # 请求头
 self.headers = {
  'Host': 'api.live.bilibili.com',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0',
 }
 # 定义POST传递的参数
 self.data = {
  'roomid': room_id,
  'csrf_token': '',
  'csrf': '',
  'visit_id': '',
 }
 # 日志写对象
 self.log_file_write = open('danmu.log', mode='a', encoding='utf-8')
 # 读取日志
 log_file_read = open('danmu.log', mode='r', encoding='utf-8')
 self.log = log_file_read.readlines()

def get_danmu(self):
 # 暂停0.5防止cpu占用过高
 time.sleep(1)
 # 获取直播间弹幕
 html = requests.post(url=self.url, headers=self.headers, data=self.data).json()
 # 解析弹幕列表
 for content in html['data']['room']:
  # 获取昵称
  nickname = content['nickname']
  # 获取发言
  text = content['text']
  # 获取发言时间
  timeline = content['timeline']
  # 记录发言
  msg = timeline + ' ' + nickname + ': ' + text
  # 判断对应消息是否存在于日志,如果和最后一条相同则打印并保存
  if msg + '\n' not in self.log:
   # 打印消息
   listb.insert(END, msg)
   listb.see(END)
   # 保存日志
   self.log_file_write.write(msg + '\n')
   # 添加到日志列表
   self.log.append(msg + '\n')
  # 清空变量缓存
  nickname = ''
  text = ''
  timeline = ''
  msg = ''

def bilibili(delay, room_id):
# 创建bDanmu实例
bDanmu = Danmu(room_id)
while True:
 # 暂停防止cpu占用过高
 time.sleep(delay)
 # 获取弹幕
 bDanmu.get_danmu()

def author():
# 弹出对话框
messagebox.showinfo(title='关于', message='作者:阿壮Jonson\n日期:2021年2月4日\n微信公众号:科技猫')

# tkinter GUI
window = tkinter.Tk()
window.title('BiliBli弹幕查看工具')
window.minsize(300, 500)
window.geometry('400x600+250+100')

# 菜单栏
menubar = tkinter.Menu(window)
# Open放在菜单栏中,就是装入容器
menubar.add_command(label='关于', command=author)
# 创建菜单栏完成后,配置让菜单栏menubar显示出来
window.config(menu=menubar)

# 滚动条
sc = tkinter.Scrollbar(window)
sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
# Listbox控件
listb = tkinter.Listbox(window, yscrollcommand=sc.set)
# 将部件放置到主窗口中
listb.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True)
# 滚动条动,列表跟着动
sc.config(command=listb.yview)

# 获取字符串(标题,提示,初始值)
room_id = simpledialog.askstring(title='请输入房间号', prompt='请输入房间号:'
        , initialvalue='21089733')
if room_id is not None:
# 创建获取弹幕线程
try:
 t = threading.Thread(target=bilibili, args=(0.5, str(room_id),))
 t.setDaemon(True)
 t.start()
except:
 print("Error: 启动失败!请检查房间号是否正确")
# 进入循环显示
window.mainloop()

编译

使用第三方包:pyinstaller

命令


pyinstaller -F -w bilibli-danmu.py

参数解释

-F,-onefile 产生单个的可执行文件

-w,--windowed,--noconsolc 指定程序运行时不显示命令行窗口(仅对 Windows 有效) PyInstaller 支持的常用选项

使用python tkinter开发一个爬取B站直播弹幕工具的实现代码

补充

执行完命令之后会在项目目录下多出dist文件夹,编译后的文件就在该文件夹下 pyinstaller 不可以跨平台编译,windows平台下只能编译成windows下的执行文件(.exe),同理mac linux也是一样

来源:https://segmentfault.com/a/1190000039183583

标签:python,tkinter,直播,弹幕
0
投稿

猜你喜欢

  • Vue使用JSEncrypt实现rsa加密及挂载方法

    2024-06-05 10:02:30
  • Python3 pywin32模块安装的详细步骤

    2023-01-20 06:42:46
  • mssql2005字符串连接方法 避免无效的连接错误

    2024-01-14 20:45:05
  • jupyter lab的目录调整及设置默认浏览器为chrome的方法

    2023-07-28 09:44:53
  • Python获取图像中像素点坐标实例代码

    2021-02-20 19:42:05
  • Django MTV和MVC的区别详解

    2023-06-08 10:15:19
  • python实现批量修改图片格式和尺寸

    2021-02-12 10:47:35
  • mysql部分替换sql语句分享

    2024-01-23 18:17:35
  • Django 通过JS实现ajax过程详解

    2023-08-17 08:00:46
  • Python 错误和异常小结

    2021-08-19 12:17:58
  • Python2与Python3的区别实例分析

    2021-01-07 11:47:17
  • Golang之defer 延迟调用操作

    2023-08-04 18:21:48
  • 页面嵌入Windows Media Player播放器代码需要注意的

    2023-07-02 17:04:48
  • python实现中文文本分句的例子

    2023-02-15 12:16:24
  • MYSQL初学者扫盲

    2009-02-27 13:15:00
  • Python如何获取多线程返回结果

    2024-01-01 23:34:28
  • python基础学习之组织文件

    2022-04-08 21:47:23
  • Django中实现点击图片链接强制直接下载的方法

    2023-08-03 13:29:19
  • Python插入Elasticsearch操作方法解析

    2021-08-30 01:47:09
  • python tkinter制作用户登录界面的简单实现

    2021-01-07 06:00:09
  • asp之家 网络编程 m.aspxhome.com