Python3–監控疫情

  • 2020 年 2 月 18 日
  • 筆記

import requests

import json

from pyecharts.charts import Map, Geo

from pyecharts import options as opts

from pyecharts.globals import GeoType, RenderType

url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'

datas = json.loads(requests.get( url =url).json()[ 'data' ])

china = datas[ 'areaTree' ][ 0 ][ 'children' ]

data = []

for i in range ( len (china)):

data.append([china[i][ 'name' ], china[i][ 'total' ][ 'confirm' ]])

confirm = datas[ 'chinaTotal' ][ 'confirm' ]

suspect = datas[ 'chinaTotal' ][ 'suspect' ]

dead = datas[ 'chinaTotal' ][ 'dead' ]

heal = datas[ 'chinaTotal' ][ 'heal' ]

lastUpdateTime = datas[ 'lastUpdateTime' ]

print (confirm, suspect, dead, lastUpdateTime)

china_total = "確診:" + str (confirm) + " 疑似:" + str (suspect) + " 死亡:" + str (dead) + " 治癒:" + str (

heal) + " 更新日期:" + lastUpdateTime

geo = (

Geo( init_opts =opts.InitOpts( width = "1200px" , height = "600px" , bg_color = "#404a59" , page_title = "全國疫情實時報告" ,

renderer =RenderType.SVG, theme = "white" )) # 設置繪圖尺寸,背景色,頁面標題,繪製類型

.add_schema( maptype = "china" , itemstyle_opts =opts.ItemStyleOpts( color = "rgb(49,60,72)" ,

border_color = "rgb(0,0,0)" )) # 中國地圖,地圖區域顏色,區域邊界顏色

.add( series_name = "geo" , data_pair =data, type_ =GeoType.EFFECT_SCATTER) # 設置地圖數據,動畫方式為漣漪特效effect scatter

.set_series_opts( # 設置系列配置

label_opts =opts.LabelOpts( is_show = False ), # 不顯示Label

effect_opts =opts.EffectOpts( scale = 6 )) # 設置漣漪特效縮放比例

.set_global_opts( # 設置全局系列配置

# visualmap_opts=opts.VisualMapOpts(min_=0, max_=sum / len(datas)), # 設置視覺映像配置,最大值為平均值

title_opts =opts.TitleOpts( title = "全國疫情地圖" , subtitle =china_total, pos_left = "center" , pos_top = "10px" ,

title_textstyle_opts =opts.TextStyleOpts( color = "#fff" )),

# 設置標題,副標題,標題位置,文字顏色

legend_opts =opts.LegendOpts( is_show = False ), # 不顯示圖例

)

)

geo.render( path = "./render.html" )

運行的效果圖