Skip to content

Scraping Bilibili Live Chat with Python

April 24, 2021

Source code:

import requests
import time
import re
import pyautogui as gui

class Danmu():
    def __init__(self):
        # Danmaku URL
        self.url = 'https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory'
        # Request headers
        self.headers = {
            'Host':'api.live.bilibili.com',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0',
        }
        # POST parameters
        self.data = {
            # Live room ID
            'roomid':'1111111',
            'csrf_token':'',
            'csrf':'',
            'visit_id':'',
        }
        # Log writer
        self.log_file_write = open('danmu.log',mode='a',encoding='UTF-8')
        # Read log
        log_file_read = open('danmu.log',mode='r',encoding='UTF-8')
        self.log = log_file_read.readlines()

    def get_danmu(self):
        # Get live room danmaku
        html = requests.post(url=self.url,headers=self.headers,data=self.data).json()
        # Parse danmaku list
        for content in html['data']['room']:
            # Get nickname
            nickname = content['nickname']
            # Get message
            text = content['text']
            # Get timestamp
            timeline = content['timeline']
            # Compose: time nickname: message
            msg = timeline+' '+nickname+': '+text
            # Check if message exists in log; print and save if new
            if msg+'\n' not in self.log:
                # Print message (danmaku info)
                print(msg)
                # Save to log
                self.log_file_write.write(msg+'\n')
                # Add to log list
                self.log.append(msg+'\n')
            # Clear variable cache
            nickname = ''
            text = ''
            timeline = ''
            msg = ''

# Create bDanmu instance
bDanmu = Danmu()
while True:
    # Sleep 0.5s to avoid high CPU usage
    time.sleep(0.5)
    # Fetch danmaku
    bDanmu.get_danmu()
Previous
Building a QQ Automation Bot with Lua and Mirai
Next
Bypassing Campus Network Authentication with UDP Port Forwarding