用python在b站发弹幕


chrome浏览器 F12开发者模式 左边停止抓包 右边清除数据

Cookie记录了“我”的账号信息

1:在av7189701视频发送666弹幕

2:在av7189701视频发送666弹幕

3:在av7189702视频发送hhhh弹幕

以上是在不同的视频发送的不同弹幕,方法为post的数据

 

第一步:发送一条弹幕

import requests #网络请求

#定义类
class SendDanMu:
    #构造函数
    def __init__(self):    #self相当于this?
        self.url = 'https://interface.bilibili.com/dmpost?cid=11730021&aid=7189702&pid=1&ct=1'
        self.cookies = {
            'Cookie':'LIVE_BUVID=AUTO3315133301403011; sid=kfqwfo12; fts=1513330387; buvid3=3EFC3F94-CFCC-4C3E-8126-1120302E7ACC32291infoc; UM_distinctid=16059882c997c-0e733ae7a5e45c-541b371f-100200-16059882c9b60; pgv_pvi=523574272; rpdid=ismkpswlwpdosokkkwkqw; finger=7360d3c2; DedeUserID=5837021; DedeUserID__ckMd5=ec0751e6f313b389; SESSDATA=b4b1e181%2C1520686410%2C7abd8a84; bili_jct=1a7f313b22996f6f6b4a783d40e23e69; _dfcaptcha=58dddd4f8a7d8b46a24b8a3153ee7fea'
        }
    #发送弹幕
    def sendDM(self,datax): #所有函数的第一个参数都是self,要写
        self.html = requests.post(self.url,data = datax,cookies = self.cookies)
        print(self.html)

senddanmu = SendDanMu()
#json 数据字典形式
data
= { 'fontsize':'25', 'pool':'0', 'mode':'1', 'color':'16777215', 'rnd':'1518095916554615', 'message':'hello',#这里是发送的弹幕信息 'playTime':'0', 'cid':'11730021', 'date':'2018-02-08 21:18:55', 'csrf':'1a7f313b22996f6f6b4a783d40e23e69' } senddanmu.sendDM(data) #<Response [200]> 200是状态码 表示pass

以上代码成功在av7189702视频中发送了hello弹幕

第二步:发送批量弹幕

import requests #网络请求
import random
import time

#定义类
class SendDanMu:
    #构造函数
    def __init__(self):    #self相当于this?
        self.url = 'https://interface.bilibili.com/dmpost?cid=11730021&aid=7189702&pid=1&ct=1'
        self.cookies = {
            'Cookie':'LIVE_BUVID=AUTO3315133301403011; sid=kfqwfo12; fts=1513330387; buvid3=3EFC3F94-CFCC-4C3E-8126-1120302E7ACC32291infoc; UM_distinctid=16059882c997c-0e733ae7a5e45c-541b371f-100200-16059882c9b60; pgv_pvi=523574272; rpdid=ismkpswlwpdosokkkwkqw; finger=7360d3c2; DedeUserID=5837021; DedeUserID__ckMd5=ec0751e6f313b389; SESSDATA=b4b1e181%2C1520686410%2C7abd8a84; bili_jct=1a7f313b22996f6f6b4a783d40e23e69; _dfcaptcha=58dddd4f8a7d8b46a24b8a3153ee7fea'
        }
    #发送弹幕
    def sendDM(self,datax): #所有函数的第一个参数都是self,要写
        self.html = requests.post(self.url,data = datax,cookies = self.cookies)
        print(self.html)

if __name__ == '__main__':
    senddanmu = SendDanMu()
    duodanmu = [
        'hello',
        'nihao',
        'hi'
    ]
    for _ in range(5):

        time.sleep(10)

        data = {
            'fontsize':'25',
            'pool':'0',
            'mode':'1',
            'color':'16777215',
            'rnd':'1518095916554615',
            'message':duodanmu[random.randint(0,2)],
            'playTime':'0.060',
            'cid':'11730021',
            'date':'2018-02-08 21:18:55',
            'csrf':'1a7f313b22996f6f6b4a783d40e23e69'
        }
        senddanmu.sendDM(data) #<Response [200]> 200是状态码 表示pass

第三步:获取弹幕

import requests #网络请求
import random
import time
import json

class GetDanMu:
    def __init__(self):
        self.url = 'https://comment.bilibili.com/31345838.xml'


    def getDM(self):
        self.danmu = requests.get(self.url)
        # print(json.load(self.danmu))
        print(self.danmu.text)

getdanmu = GetDanMu()
getdanmu.getDM()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM