本文最后更新于462 天前,其中的信息可能已经过时,如有错误请发送邮件到3368129372@qq.com
以bilibili直播弹幕为例
导入相关库
import requests,json,time
设置请求头
url = 'https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory?roomid=26568219&room_type=0'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
}
数据处理
response = json.loads(requests.get(headers=headers, url=url).text)
response = response['data']['room']
for i in response:
text = i['text']
name = i['nickname']
timeline = i['timeline']
result = f'[{timeline}] {name}: {text}'
if result not in item:
item.append(result)
print(result)
time.sleep(0.5)
将读取到的str类型(json)转化为dict数据,并从中取出想要的数据进行输出