数据源是从腾讯新闻得到的
打开开发者工具的network,F12刷新界面就可以看到一些包
这个为我们要的链接
分析json的格式,解析数据
url='https://view.inews.qq.com/g2/getOnsInfo?name=disease_other'
get_China='https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
class item:
def __init__(self):
self.date=list()#日期
self.newAddConfirm = list()#新增确诊
self.confirm=list()#累计确诊
self.dead=list()#死亡
self.heal=list()#治愈
self.deadRate=list()#死亡率
self.healRate=list()#治愈率
Data_Box=item()#数据盒子
def GetHtmlText(url):
try:
res = requests.get(url,timeout = 30)
res.raise_for_status()
res.encoding = res.apparent_encoding
return res.text
except:
return "Error"
data = json.loads(requests.get(url=url).json()['data'])
foreignlist = data["globalDailyHistory"]
json_data=[]
for global_data in foreignlist:
#获取每日总信息
Data_Box.confirm.append(global_data["all"]["confirm"])
Data_Box.date.append(global_data["date"])
Data_Box.newAddConfirm.append(global_data["all"]["newAddConfirm"])
Data_Box.dead.append(global_data["all"]["dead"])
Data_Box.heal.append(global_data["all"]["heal"])
Data_Box.deadRate.append(global_data["all"]["deadRate"])
Data_Box.healRate.append(global_data["all"]["healRate"])
#json_data.append({"name":global_data["name"],"confirm":global_data["confirm"],"confirmAdd":global_data["confirmAdd"],"dead":global_data["dead"],"heal":global_data["heal"]})
length=len(Data_Box.date)
接下来是使用pyecharts timeline轮播线可视化
tl = Timeline()
tl.add_schema(is_auto_play = True,play_interval = 100,is_loop_play = False)
confirm = np.zeros((length,length))
for i in range(0,length):
confirm[i][:(i+1)]=Data_Box.confirm[:(i+1)]
for n in range(0,length):
# confirm=list()
# df=pd.DataFrame(for )
bar = (
Line()
.add_xaxis(Data_Box.date)
.add_yaxis("累计确诊数", Data_Box.confirm[:(n+1)])
.extend_axis( yaxis=opts.AxisOpts())
.set_series_opts(areastyle_opts=opts.AreaStyleOpts(opacity=0.5),label_opts=opts.LabelOpts(is_show=False) )
.set_global_opts(title_opts=opts.TitleOpts("海外{}累计确诊数".format(Data_Box.date[n])))
)
tl.add(bar,Data_Box.date[n])
tl.render("timeline_bar.html")
完整代码
from pyecharts import options as opts
from pyecharts.charts import Line, Timeline
from pyecharts.faker import Faker
import requests
import json
import openpyxl
import time
from datetime import datetime
import pandas as pd
import numpy as np
nowdate =datetime.now().strftime('%m-%d')
save_path='timing_foreign'+nowdate+'.xlsx'
url='https://view.inews.qq.com/g2/getOnsInfo?name=disease_other'
get_China='https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
class item:
def __init__(self):
self.date=list()#日期
self.newAddConfirm = list()#新增确诊
self.confirm=list()#累计确诊
self.dead=list()#死亡
self.heal=list()#治愈
self.deadRate=list()#死亡率
self.healRate=list()#治愈率
Data_Box=item()#数据盒子
def GetHtmlText(url):
try:
res = requests.get(url,timeout = 30)
res.raise_for_status()
res.encoding = res.apparent_encoding
return res.text
except:
return "Error"
data = json.loads(requests.get(url=url).json()['data'])
foreignlist = data["globalDailyHistory"]
json_data=[]
for global_data in foreignlist:
#获取每日总信息
Data_Box.confirm.append(global_data["all"]["confirm"])
Data_Box.date.append(global_data["date"])
Data_Box.newAddConfirm.append(global_data["all"]["newAddConfirm"])
Data_Box.dead.append(global_data["all"]["dead"])
Data_Box.heal.append(global_data["all"]["heal"])
Data_Box.deadRate.append(global_data["all"]["deadRate"])
Data_Box.healRate.append(global_data["all"]["healRate"])
#json_data.append({"name":global_data["name"],"confirm":global_data["confirm"],"confirmAdd":global_data["confirmAdd"],"dead":global_data["dead"],"heal":global_data["heal"]})
length=len(Data_Box.date)
tl = Timeline()
tl.add_schema(is_auto_play = True,play_interval = 100,is_loop_play = False)
confirm = np.zeros((length,length))
for i in range(0,length):
confirm[i][:(i+1)]=Data_Box.confirm[:(i+1)]
for n in range(0,length):
# confirm=list()
# df=pd.DataFrame(for )
bar = (
Line()
.add_xaxis(Data_Box.date)
.add_yaxis("累计确诊数", Data_Box.confirm[:(n+1)])
.extend_axis( yaxis=opts.AxisOpts())
.set_series_opts(areastyle_opts=opts.AreaStyleOpts(opacity=0.5),label_opts=opts.LabelOpts(is_show=False) )
.set_global_opts(title_opts=opts.TitleOpts("海外{}累计确诊数".format(Data_Box.date[n])))
)
tl.add(bar,Data_Box.date[n])
tl.render("timeline_bar.html")