Python實現直播彈幕自動發送功能

前言

今天制作的這一款能在B站能指定直播間、自動發彈幕的功能的腳本,因為沒做那麼多的功能,所以代碼很簡單,適合剛入門的同學學習

先打開一個直播間
按F12打開開發者工具
發送彈幕666
在send裡找到我們所需的鏈接

發送請求

需要加上headers和data

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%3D%3D; 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%2C1661245310%2C91227*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)

循環發彈幕

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',
    }

做個交互界面

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()

將前面發送彈幕的代碼設成函數,再調用

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)

最後看看效果怎麼樣

看來效果很成功,目前功能很簡單,還可以添加很多,同學們也可以自己試試往上加東西

到此這篇關於Python實現直播彈幕自動發送的文章就介紹到這瞭,更多相關Python直播彈幕自動發送內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: