Python实现直播弹幕自动发送

2022-03-30 20:33:23 浏览数 (2)

前言

今天制作的这一款弹幕机器人,是能在B站能指定直播间、自动发弹幕的功能 因为没做那么多的功能,所以代码很简单,适合刚入门的同学学习

直接开始代码

找到发送弹幕的接口

先打开一个直播间 按F12打开开发者工具 发送弹幕666 在send里找到我们所需的链接

发送请求

需要加上 headers 和 data

代码语言:javascript复制
import requests

url = 'https://api.live.bilibili.com/msg/send'
data = {
    'bubble': '0',
    'msg': '666666',
    'color': '16777215',
    'mode': '1',
    'fontsize': '25',
    'rnd': '1646460756',
    'roomid': '545068',
    'csrf': 'a121a39614a5131b700e07334c3e2f2e',
    'csrf_token': 'a121a39614a5131b700e07334c3e2f2e',
}

headers = {
    'cookie': 'buvid3 = B68B2187 - 4C3E - 4466 - A896 - FBF9B292099B190963infoc;LIVE_BUVID = AUTO4115757254257055;rpdid = | (umu | ulY)JJ0J'ul~l~klRJ); Hm_lvt_ff57561a8cad2056ebeb8790418f7c80=1617598823; dy_spec_agreed=1; fingerprint_s=72d878d168cc36c3e67084f9ab1b28bf; kfcFrom=SIXIN; video_page_version=v_old_home; _uuid=838215105-B7C3-D1D3-8866-D41AAC22362C56007infoc; CURRENT_BLACKGAP=0; blackside_state=0; buvid_fp_plain=undefined; buvid4=475D6CE2-93EF-3AD0-EBCD-C528FD289A2218219-022012015-hoqbRiv5RkzgH6uKZsx6eQ==; i-wanna-go-back=-1; fingerprint3=282838a0bbd808eda8dde1558fccc94c; CURRENT_QUALITY=80; bp_video_offset_296816901=629971213059358700; sid=jtva59n5; fingerprint=416f0a37448a1faf863af4402998db67; DedeUserID=523606542; DedeUserID__ckMd5=909861ec223d26d8; SESSDATA=c666f798,1661245310,91227*21; bili_jct=ac2dd02c170854b995d5a6a601d973ea; buvid_fp=416f0a37448a1faf863af4402998db67; b_ut=5; bp_t_offset_523606542=630736739432923172; bp_video_offset_523606542=631076973870841894; innersign=0; b_lsid=B77BFA43_17F2FE6E01B; CURRENT_FNVAL=80; _dfcaptcha=fa1b3951be7c6744827b636f6c4935c9; Hm_lvt_8a6e55dbd2870f0f5bc9194cddf32a02=1645778002; Hm_lpvt_8a6e55dbd2870f0f5bc9194cddf32a02=1645778002; PVID=7',
    'origin': 'https://live.bilibili.com',
    'referer': 'https://live.bilibili.com/blanc/1029?liteVersion=true',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
}
response = requests.post(url=url, data=data, headers=headers)
print(response.status_code)

循环发弹幕

代码语言:javascript复制
import random
import time

lis_text = ['666', '主播真厉害',
            '爱了,爱了',
            '关注走一走,活到99',
            '牛逼!!!',
            '秀儿,是你吗?']

while True:
    time.sleep(2)
    send_meg = random.choice(lis_text)
    roomid = entry.get()
    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': 'ac2dd02c170854b995d5a6a601d973ea',
        'csrf_token': 'ac2dd02c170854b995d5a6a601d973ea',
    }

做个交互界面

代码语言:javascript复制
from tkinter import *

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, heigh=15)
text.grid(row=2, columnspan=2)

root.mainloop()

将前面发送弹幕的代码设成函数,再调用

代码语言:javascript复制
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)

最后看看效果怎么样

看来效果很成功

0 人点赞