import requests import re def get_response(html_url): headers = { 'cookie': '你自己的 cookie', 'origin': 'https://www.bilibili.com', 'eferer': '视频的具体链接', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36' } response = requests.get(url=html_url, headers=headers) return response def get_danmaku(video_id): # 获取弹幕日期列表 date_url = f'https://api.bilibili.com/x/v2/dm/history/index?type=1&oid={video_id}&month=2024-09' # 这里假设是 2024 年 9 月的弹幕,你可以根据实际情况修改 date_response = get_response(date_url) json_data = date_response.json() date_list = json_data['data'] danmaku_list = [] for date in date_list: danmaku_url = f'https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid={video_id}&date={date}' html_data = get_response(danmaku_url).text # 使用正则表达式匹配中文字符获取弹幕内容 result = re.findall(".?((\u4e00-\u9fa5)+).?", html_data) for i in result: danmaku_list.append(i[0]) return danmaku_list