用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