代码如下:
import random
import time
import requests
from tkinter import *# 弹幕内容列表
# lis_text = [
# '京口瓜洲一水间,钟山只隔数重山。','君不见黄河之水天上来,奔流到海不复回。','起舞弄清影,何似在人间!'
# ]
lis_text = ['床前明月光,疑是地上霜。','举头望明月,低头思故乡。','白日依山尽,苍苍云海间。','长安一片月,万户千门入梦来。','君不见,黄河之水天上来,奔流到海不复回。','大漠孤烟直,长河落日圆。','月落乌啼霜满天,江枫渔火对愁眠。','无可奈何花落去,似曾相识燕归来。','青山遮不住,毕竟东流去。','春眠不觉晓,处处闻啼鸟。','人闲桂花落,夜静春山空。','桃花潭水深千尺,不及汪伦送我情。','起舞弄清影,何似在人间!','白云千载空悠悠,苍狗万年前已去。','月出惊山鸟,时鸣春涧猿。','千里共婵娟。','天阶夜色凉如水,卧看牵牛织女星。','欲穷千里目,更上一层楼。','君自故乡来,应知故乡事。','长风破浪会有时,直挂云帆济沧海。','会当凌绝顶,一览众山小。','相见时难别亦难,东风无力百花残。','鸟宿池边树,僧敲月下门。','相思相见知何日?此时此夜难为情。','两情若是久长时,又岂在朝朝暮暮。','月明点滴垂杨影,风送声声入夜愁。','云青青兮欲雨,水澹澹兮生烟。','问君何能尔?心远地自偏。','身无彩凤双飞翼,心有灵犀一点通。','莫愁前路无知己,天下谁人不识君。','日照香炉生紫烟,遥看瀑布挂前川。','独在异乡为异客,每逢佳节倍思亲。','白雪歌送武判官归京。','老夫聊发少年狂,左牵黄,右擎苍。','过故人庄。','夜来风雨声,花落知多少。','月明点滴垂杨影,风送声声入夜愁。','春色满园关不住,一枝红杏出墙来。','乱花渐欲迷人眼,浅草才能没马蹄。','落霞与孤鹜齐飞,秋水共长天一色。','一曲肝肠断,天涯何处觅知音。','夜来风雨声,花落知多少。','青山遮不住,毕竟东流去。','白云千载空悠悠,苍狗万年前已去。','天净沙·秋思。','四面楚歌声,孤舟蓑笠翁。','月明点滴垂杨影,风送声声入夜愁。','问君何能尔,心远地自偏。','落霞与孤鹜齐飞,秋水共长天一色。','一曲肝肠断,天涯何处觅知音。','会当凌绝顶,一览众山小。','相见时难别亦难,东风无力百花残。','身无彩凤双飞翼,心有灵犀一点通。','独在异乡为异客,每逢佳节倍思亲。','月明点滴垂杨影,风送声声入夜愁。','举头望明月,低头思故乡。','青山遮不住,毕竟东流去。','天净沙·秋思。','两情若是久长时,又岂在朝朝暮暮。','鸟宿池边树,僧敲月下门。','白日依山尽,苍苍云海间。','桃花潭水深千尺,不及汪伦送我情。','人闲桂花落,夜静春山空。','千里共婵娟。','会当凌绝顶,一览众山小。','相思相见知何日,此时此夜难为情。','春色满园关不住,一枝红杏出墙来。','日照香炉生紫烟,遥看瀑布挂前川。',
]# 发送弹幕的函数
def send():roomid = entry.get()while True:time.sleep(2)send_meg = random.choice(lis_text)ti = int(time.time())url = 'https://api.live.bilibili.com/msg/send'data = {'color': '16777215','fontsize': '25','mode': '1','msg': send_meg,'rnd': '{}'.format(ti),'roomid': '{}'.format(roomid),'bubble': '0','csrf': '1faf50afcb4af577b8055c2dc9ed307e','csrf_token': '1faf50afcb4af577b8055c2dc9ed307e',}headers = {'cookie': 'buvid3=8FF25B58-E8CE-B701-ABA6-CB59807E762342620infoc; b_nut=1726995842; _uuid=5FE9F526-5F10E-1BF4-10BC7-29556222C310B44134infoc; enable_web_push=DISABLE; CURRENT_FNVAL=4048; rpdid=0zbfAI3t8z|14StjZ6zP|3n|3w1SSpZu; fingerprint=f43d51552382a59214c6fc0e36c79332; buvid_fp_plain=undefined; DedeUserID=109032638; DedeUserID__ckMd5=35b6548fc51e62d5; header_theme_version=CLOSE; CURRENT_QUALITY=80; buvid4=9C16DA79-A3B4-6FE7-7CDE-BAF3BD30116A43946-024092209-TOH4gK3GlusNelHrt2PUtsEkMXbOO3uztQusaRGtRTCkem3HNhRbjO0rGxGCeE%2FR; buvid_fp=f43d51552382a59214c6fc0e36c79332; SESSDATA=ea3dfb68%2C1742899141%2Cc55cd%2A91CjBvWsSDMtXnO1k6o5NxwSWA_MqJffnS3iolua7_QSmFghHQNpBAdMxQHFILOptbj0sSVkg3aTFLenJicEVORFgxWnBfVWZZd2dTSjZ0R1Y2N21rVGVNdG9FOUxQajY3ck4wYmgwbFdCQ044SGlpVF83QS1jTTZmLXlSZjlFcWl1aWNYdXlsbHh3IIEC; bili_jct=1faf50afcb4af577b8055c2dc9ed307e; bp_t_offset_109032638=981503033336659968; b_lsid=10AC6B176_1923430743A; home_feed_column=5; browser_resolution=1920-937; sid=phoqauh2; LIVE_BUVID=AUTO2517274524497577; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3Mjc3MTE2NTAsImlhdCI6MTcyNzQ1MjM5MCwicGx0IjotMX0.xDfSSqnl6fH-G2aS_crzybjBYMLEcgESnmglJCfo2Ok; bili_ticket_expires=1727711590; PVID=2','origin': 'https://live.bilibili.com','referer': f'https://live.bilibili.com/{roomid}?spm_id_from=333.1007.0.0','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',}response = requests.post(url=url, data=data, headers=headers)print(f"发送弹幕: {send_meg} - 状态码: {response.status_code}")# 创建主窗口
root = Tk()
root.title('B站自动发送弹幕')
root.geometry('560x450+400+200')label = Label(root, text='请输入房间号:', font=('华文行楷', 20))
label.grid()entry = Entry(root, font=('隶书', 20))
entry.grid(row=0, column=1)text = Listbox(root, font=('隶书', 16), width=50, height=15)
text.grid(row=2, columnspan=2)# 创建按钮
button1 = Button(root, text='开始发送', font=('隶书', 15), command=send)
button1.grid(row=3, column=0)button2 = Button(root, text='退出程序', font=('隶书', 15), command=root.quit)
button2.grid(row=3, column=1)# 运行主循环
root.mainloop()