python實(shí)現(xiàn)b站直播自動(dòng)發(fā)送彈幕功能
· Python 3.6
· Pycharm
相關(guān)模塊使用import requestsimport timefrom tkinter import *import random目標(biāo)i網(wǎng)頁分析
首先你要登陸B(tài)站賬號,然后隨便點(diǎn)擊一個(gè)直播間,這里建議先選擇人氣少的,彈幕少的,這樣方便查看效果
如上圖所示,先打開開發(fā)者工具,定位到xhr輸入發(fā)送內(nèi)容,點(diǎn)擊發(fā)送,會(huì)有一個(gè)post請求的send數(shù)據(jù)接口。
所以只需要請求這個(gè)數(shù)據(jù)接口即可發(fā)送彈幕。就是正常的時(shí)候爬取數(shù)據(jù),使用requests請求網(wǎng)頁一樣,一般情況大家都是使用的get請求,這里則是需要使用post請求。
之后,只要給請求的時(shí)候來一個(gè)死循環(huán),那么就可以一直發(fā)送彈幕了,然后再自定義一個(gè)彈幕內(nèi)容,讓它每次都是隨機(jī)抽選一句話發(fā)送即可。
完整代碼:import requestsimport timefrom tkinter import *import randomlis_text = [’666’, ’主播真厲害’, ’愛了,愛了’, ’關(guān)注走一走,活到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’: ’復(fù)制自己的’, ’csrf’: ’復(fù)制自己的’, } headers = { ’cookie’: ’使用你自己的cookie’, ’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, ’第{}條彈幕發(fā)送成功’.format(a)) # 文本框滾動(dòng) text.see(END) # 更新 text.update() text.insert(END, ’發(fā)送內(nèi)容:{}’.format(send_meg))root = Tk()root.title(’B站自動(dòng)發(fā)送彈幕’)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=’開始發(fā)送’, 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實(shí)現(xiàn)b站直播自動(dòng)發(fā)送彈幕的詳細(xì)內(nèi)容,更多關(guān)于python 自動(dòng)發(fā)送彈幕的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. IntelliJ IDEA導(dǎo)入jar包的方法2. Python requests庫參數(shù)提交的注意事項(xiàng)總結(jié)3. vue-electron中修改表格內(nèi)容并修改樣式4. python ansible自動(dòng)化運(yùn)維工具執(zhí)行流程5. 匹配模式 - XSL教程 - 46. python操作mysql、excel、pdf的示例7. JavaScript中l(wèi)ayim之整合右鍵菜單的示例代碼8. SpringBoot參數(shù)校驗(yàn)與國際化使用教程9. 通過Python pyecharts輸出保存圖片代碼實(shí)例10. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法
