You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pythontool/bilibili/space.py

35 lines
1.4 KiB

import json
import time
import requests
host_uid = 207539637
cards = []
offset_dynamic_id = 0
while True:
url = f"https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history?visitor_uid=0&host_uid={host_uid}&offset_dynamic_id={offset_dynamic_id}&need_top=1&platform=web"
print(f'url={url}')
res = requests.get(url)
if res.status_code == 200:
res_json = json.loads(res.content)
print('解析成功')
try:
if res_json["data"] and res_json["data"]["cards"]:
for card in res_json["data"]["cards"]:
if card["card"]:
card_json = json.loads(card["card"])
if "item" in card_json and "description" in card_json["item"] and "弹幕彩蛋" in card_json["item"][
"description"]:
print(card_json["item"]["description"])
cards.append(card_json["item"]["description"])
if res_json["data"]["cards"][-1]["desc"] and res_json["data"]["cards"][-1]["desc"]["dynamic_id_str"]:
offset_dynamic_id = res_json["data"]["cards"][-1]["desc"]["dynamic_id_str"]
time.sleep(3)
print('添加动态')
except KeyError as e:
break
else:
raise Exception(f"status_code={res.status_code}")
with open(file='test.json', mode='w', encoding='UTF-8') as f:
f.write(str(cards))