Day67 数据可视化之调用API并可视化
API:类似接口,在后端的数据可以通过API传递给前端,前端可以实现数据的可视化。与HTTP网页爬虫相比,API接口已经存储好数据,不太需要去解析网页。
参考文章:https://blog.csdn.net/qq_44332894/article/details/109862654
https://blog.csdn.net/weixin_43944997/article/details/105502469 https://www.liaoxuefeng.com/wiki/1016959663602400/1017639890281664
先用bilibili番剧进行数据提取
选用番剧列表2020年会员专享番,提取一页再进行可视化。
import requests
import re
import pygal
from pygal.style import LightColorizedStyle as LCS
from pygal.style import LightenStyle as LS
"""B站2020年付费会员番剧索引页数据"""
URL='https://api.bilibili.com/pgc/season/index/result?season_version=-1&area=-1&is_finish=-1©right=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&order=3&st=1&sort=0&page=1&season_type=1&pagesize=20&type=1'
r=requests.get(URL)
print("Status code:",r.status_code)
response_dict=r.json()
print(response_dict.keys())
print("list",response_dict['data']['list']) #字典列表
repo_dicts=response_dict['data']['list']
names,numb=[],[]
for repo_dict in repo_dicts:
print("标志:",repo_dict['badge']) #独家还是会员专享
print("链接:",repo_dict['link']) #视频观看链接
pop=re.findall('\d+\.\d*',repo_dict['order']) #浮点型匹配
if len(pop)==False:
pop=re.findall(r'\d+',repo_dict['order']) #为空列表,用整数匹配
float_pop=float(pop[0])
#追番人数,先从列表里取出字符串,再用正则表达式提取数字
print("追番人数:",float_pop)
print("名字:",repo_dict['title']) #番名
print("\n")
names.append(repo_dict['title']) #x轴
numb.append(float_pop) #Y轴
输出:
Status code: 200
dict_keys(['code', 'data', 'message'])
list [{
'badge': '独家', 'badge_info': {
'bg_color': '#00C0FF', 'bg_color_night': '#0B91BE', 'text': '独家'}, 'badge_type': 1, 'cover': 'http://i0.hdslb.com/bfs/bangumi/image/4179b4398bad6f92e876e352cae21be7b8ceb8bf.png', 'index_show': '全26话', 'is_finish': 1, 'link': 'https://www.bilibili.com/bangumi/play/ss26801', 'media_id': 22718131, 'order': '917.3万追番', 'order_type': 'fav_count', 'season_id': 26801, 'season_type': 1, 'title': '鬼灭之刃', 'title_icon': ''}, {
'badge': '独家', 'badge_info': {
'bg_color': '#00C0FF', 'bg_color_night': '#0B91BE', 'text': '独家'}, 'badge_type': 1, 'cover': 'http://i0.hdslb.com/bfs/bangumi/image/962b27ff452b5daf98038a10a6dad3c134916e7d.png', 'index_show': '更新至第22话', 'is_finish': 0, 'link': 'https://www.bilibili.com/bangumi/play/ss34004', 'media_id': 28229443, 'order': '783.3万追番', 'order_type': 'fav_count', 'season_id': 34004,