Python写一个自动发送直播弹幕的工具,非常好用

昨晚回家,表弟在看LOL直播,看得我气不打一处来,差点就想锤他。

身为程序员的表弟,看直播发弹幕居然还在手动发,当时我就用Python写了一个自动发送弹幕的脚本送给他用。

好了话不多说,我们直接开搞!

先看看效果

名字我就打码了,当然名字不是关键,我直接截图展示算了,GIF的话,太麻烦了。

接下来我们要准备好你想发送的弹幕内容

这个我都是随便打的,大家可以自己慢慢敲自己想要的内容。


内容输入完,直接保存就好了,名字可以像我这样,当然你也可以自己取。

代码展示

话不多说,我们来看看代码。

使用的模块

import requests
import time
from tkinter import *
import random

弹幕

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

主要实现代码

def send():
    a = 0
# 我还录了专门的视频讲解,跟完整代码都一起打包好了,直接在文末名片自取即可。
    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_token': '08d11cd34efbf3da0d2138d562145e5c',
            'csrf': '08d11cd34efbf3da0d2138d562145e5c',
        }
 
        headers = {
            'cookie': '_uuid=50D22ECF-208D-9409-DEA1-0B3EA3F74AB793744infoc; buvid3=A0FE83C2-5981-40DC-B0E2-C74A37227ECF155818infoc; rpdid=|(umuummlkY~0J\'ulm|ullmll; sid=kr4i59d5; LIVE_BUVID=AUTO3215909029132687; blackside_state=1; CURRENT_FNVAL=80; DedeUserID=406732493; DedeUserID__ckMd5=48c43aca436bb747; SESSDATA=204f478b%2C1615703177%2C53385*91; bili_jct=08d11cd34efbf3da0d2138d562145e5c; dy_spec_agreed=1; Hm_lvt_8a6e55dbd2870f0f5bc9194cddf32a02=1598946515,1600327358; bp_video_offset_406732493=463816176887860111; _dfcaptcha=90896a21dabbab6ef641f2e393b46913; bsource=search_baidu; PVID=6',
            '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',
        }
        a += 1
        response = requests.post(url=url, data=data, headers=headers)
        print(response)
        text.insert(END, '第{}条弹幕发送成功'.format(a))
        # 文本框滚动
        text.see(END)
        # 更新
        text.update()
        text.insert(END, '发送内容:{}'.format(send_meg))

TK界面

root = Tk()
root.title('B站自动发送弹幕')
root.geometry('560x450+400+200')

label = Label(root, text='请输入房间ID:', 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)
 
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()

好了,兄弟们赶紧去试试吧,说不定下次遇到直播抽奖,咱们就可以直接自动发弹幕搞一波!

您可以使用Python的requests库向斗鱼API发送POST请求来发送弹幕。首先,您需要使用与斗鱼API提供的HTTP头以及您的斗鱼账号和房间号码进行身份验证。然后,您需要构建一个JSON对象,其中包含您要发送的消息和房间号码。最后,您需要将此JSON对象作为数据发送到API的弹幕发送端点。下面是一个简单的示例代码: ``` import requests import time # 身份验证信息 username = "您的斗鱼账号" password = "您的斗鱼密码" room_id = "要发送弹幕的房间号" # 获取登录token login_url = "https://passport.douyu.com/lapi/passport/loginByPassport" login_data = { "username": username, "password": password, "type": "normal" } login_response = requests.post(login_url, json=login_data).json() token = login_response["data"]["token"] # 发送弹幕 send_url = "https://www.douyu.com/betard/{room_id}/sendmsg".format(room_id=room_id) headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3", "Cookie": "acf_did=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;acf_uid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;dy_did=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;dy_uid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;smidV2=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;wan_auth37wan=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;acf_auth=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;acf_username=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;acf_userpwd=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;auth-time=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "Referer": "https://www.douyu.com/{room_id}".format(room_id=room_id), "content-type": "application/json", "token": token, } while True: # 发送弹幕内容 msg = "Hello, world!" data = { "msg": msg, "roomid": int(room_id), "cid": int(time.time()*1000), "ct": 0, "type": "chatmsg" } response = requests.post(send_url, json=data, headers=headers) time.sleep(10) # 10秒发送一次弹幕 ``` 请将上面代码中的xxxxxx替换成您的实际信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值