from typing import List
import requests
import json
import sys
url = 'https://api.bilibili.com/x/web-interface/popular'
querystring = {'ps': '20', 'pn': '1'}
payload = ''
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
}
response = requests.get(url, data=payload, headers=headers, params=querystring)
response_json = response.json()
if not response_json['code'] == 0:
print('获取数据失败')
sys.exit(0)
data: List[dict] = []
for item in response_json['data']['list']:
data.append({
'title': item['title'],
'url': f'https://www.bilibili.com/video/{item["bvid"]}',
'author': item['owner']['name']
})
print(json.dumps(data, ensure_ascii=False, indent=4))