前言
媳妇儿玩起了bilibili直播,我就顺便研究下利用python发送弹幕,以表支持~,主要思路通过发送http请求。
发送弹幕
1. 查询http请求
首先登录b站,进入直播间,打开开发者工具,先在直播间发送一条弹幕。
可以看到请求的url地址,请求方式是post,以及请求的表单数据:
2. 查询自己账号cookie和user-agent信息
打开开发者工具,进入个人中心/首页:
3. 代码
import requests
import time
class bullet_screen():
def __init__(self, room_id):
self.room_id = room_id
self.send_url = "https://api.live.bilibili.com/msg/send"
self.cookie = {
'Cookie' : '填入上图查询到的cookie'}
self.header = {
'User-Agent' : '填入上图查询到的userAgent'}
self.send_data = {
'color': 16777215,
'fontsize': 25,
'mode': 1,
'msg': '123',
'rnd': 1582471255,
'roomid': room_id,
'bubble': 0,
'csrf_token': '44d0e7f3eaea7ff05774822e8eaf49b2',
'csrf': '44d0e7f3eaea7ff05774822e8eaf49b2'
}
def send(self, msg):
self.send_data['rnd'] = time.time() #获得当前的时间戳
self.send_data['msg'] = msg
reponse = requests.post(self.send_url