写在前面
发弹幕只是其中一个小小的功能,还可以自动点赞、收藏、投币、自动播放、私信等等,但是我们只演示这个,其它的不做展示。
实现步骤
先打开一个视频或者直播,F12打开开发者工具,点击network。
然后点这个清空一下
再发送一个弹幕,然后可以看到这个send,有一个post请求。
点击payload可以看到我们刚刚发送的弹幕相关数据
首先导入模块
import random
import time
这是我们的url
url = 'https://api.live.bi******.com/msg/send'
某些原因我就不打全了
接收一下data参数
data = {
'bubble': '0',
'msg': 6666,
'color': '16777215',
'mode': '1',
'fontsize': '25',
'rnd': '1640181316',
'roomid': '22819508',
'csrf': '131d24bf2b92a3609208ed83758ab8f2',
'csrf_token': '131d24bf2b92a3609208ed83758ab8f2',
}
然后headers请求头这里,把cookie什么的都给它加上去。
headers = {
'cookie': '_uuid=CF79473B-0F83-6087-BCB0-23A7E36C479296281infoc; buvid3=8AEBDC64-0CEE-4F6F-9866-B1508FAB2871148805infoc; blackside_state=1; rpdid=|(kmJY|kmu|)0J\'uYJRYkJu|Y; CURRENT_QUALITY=32; buvid_fp=8AEBDC64-0CEE-4F6F-9866-B1508FAB2871148805infoc; video_page_version=v_old_home; sid=abkn3and; i-wanna-go-back=-1; b_ut=5; bp_video_offset_65901796=602085772740229500; LIVE_BUVID=AUTO5916400017585242; CURRENT_BLACKGAP=1; CURRENT_FNVAL=2000; b_lsid=FB21108D7_17DE26BCC89; fingerprint=3c7949ca118951ae1be0b5b37b321306; buvid_fp_plain=556F173C-C5B7-4583-86F9-FD1074EB421D143076infoc; DedeUserID=523606542; DedeUserID__ckMd5=909861ec223d26d8; SESSDATA=cec4dda4%2C1655733287%2Cc5b88*c1; bili_jct=131d24bf2b92a3609208ed83758ab8f2; innersign=0; _dfcaptcha=fbb016616944e9b94331aeb2627c9d3d; PVID=3',
'origin': 'https://live.bi****.com',
'referer': 'https://live.bi****.com/',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
}
接收数据
response = requests.post(url=url, data=data, headers=headers)
输出
print(response.text)
这是最最最简单的版本,就四行代码就可以了。
视频运行一下,发了个你好。
那如果要发多一点呢?
给它一个列表
import random
import time
while True:
lis = ['主播666', '主播真帅', '666']
word = random.choice(lis)
url = 'https://api.live.bi****.com/msg/send'
data = {
'bubble': '0',
'msg': word,
'color': '16777215',
'mode': '1',
'fontsize': '25',
'rnd': '1640181316',
'roomid': '22819508',
'csrf': '131d24bf2b92a3609208ed83758ab8f2',
'csrf_token': '131d24bf2b92a3609208ed83758ab8f2',
}
headers = {
'cookie': '_uuid=CF79473B-0F83-6087-BCB0-23A7E36C479296281infoc; buvid3=8AEBDC64-0CEE-4F6F-9866-B1508FAB2871148805infoc; blackside_state=1; rpdid=|(kmJY|kmu|)0J\'uYJRYkJu|Y; CURRENT_QUALITY=32; buvid_fp=8AEBDC64-0CEE-4F6F-9866-B1508FAB2871148805infoc; video_page_version=v_old_home; sid=abkn3and; i-wanna-go-back=-1; b_ut=5; bp_video_offset_65901796=602085772740229500; LIVE_BUVID=AUTO5916400017585242; CURRENT_BLACKGAP=1; CURRENT_FNVAL=2000; b_lsid=FB21108D7_17DE26BCC89; fingerprint=3c7949ca118951ae1be0b5b37b321306; buvid_fp_plain=556F173C-C5B7-4583-86F9-FD1074EB421D143076infoc; DedeUserID=523606542; DedeUserID__ckMd5=909861ec223d26d8; SESSDATA=cec4dda4%2C1655733287%2Cc5b88*c1; bili_jct=131d24bf2b92a3609208ed83758ab8f2; innersign=0; _dfcaptcha=fbb016616944e9b94331aeb2627c9d3d; PVID=3',
'origin': 'https://live.bi****.com',
'referer': 'https://live.bi****.com/',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
}
response = requests.post(url=url, data=data, headers=headers)
print(response.text)
time.sleep(5)