-
Notifications
You must be signed in to change notification settings - Fork 1
/
bili_online.py
50 lines (45 loc) · 1.73 KB
/
bili_online.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from re import U
import requests
import json
from lxml import html
from colorama import init
from colorama import Fore, Style
# def get_bili_online():
# page = requests.get('https://www.bilibili.com/video/online.html')
# tree = html.fromstring(page.content)
# # 任意位置class=ebox的div节点
# ebox_list = tree.xpath('//div[@class="ebox"]')
# for ebox in ebox_list:
# title = ebox.xpath('.//a/p[@class="etitle"]/text()')[0].encode('ISO-8859-1').decode('utf-8')
# author = ebox.xpath('.//a[@class="author"]/text()')[0].encode('ISO-8859-1').decode('utf-8')
# online_num = ebox.xpath('.//p/b/text()')[0]
# url = "https:" + ebox.xpath('.//a/@href')[0]
# print(
# Fore.CYAN + "Up主:" + author + Style.RESET_ALL +
# " - " + title +
# " - " + Fore.YELLOW + online_num + Style.RESET_ALL +
# " - " + Style.DIM + url
# )
def get_bili_online():
url = "https://api.bilibili.com/x/web-interface/online/list"
response = requests.get(url)
assert(response.status_code == 200)
online_list = json.loads(response.text)['data']
# print(online_list)
for card in online_list:
owner = card['owner']['name']
title = card['title']
category = card['tname']
online_count = card['online_count']
link = card['short_link']
# print(owner)
print(
Fore.CYAN + "Up主:" + owner + Style.RESET_ALL +
" - " + title +
" - " + Fore.YELLOW + category + Style.RESET_ALL +
" - " + Style.DIM + link + Style.RESET_ALL +
" - " + Style.DIM + str(online_count)
)
if __name__ == '__main__':
init(autoreset=True)
get_bili_online()