來自:https://blog.csdn.net/shineych/article/details/104231072?utm_source=distribute.pc_category.none-task
from pyquery import PyQuery as pq from bs4 import BeautifulSoup import datetime import numpy as np import re import requests import time, json, requests import jsonpath from pyecharts.charts import Map import pyecharts.options as opts #%% shenname="浙江" # 全國疫情地區分布(各省確診病例) def catch_cn_disease_dis(): timestamp = '%d'%int(time.time()*1000) url_area = ('https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5' '&callback=&_=') + timestamp world_data = json.loads(requests.get(url=url_area).json()['data']) china_data = jsonpath.jsonpath(world_data, expr='$.areaTree[0].children[*]') ls_province_names = jsonpath.jsonpath(china_data, expr='$[*].name') ls_confirm_vals = jsonpath.jsonpath(china_data, expr='$[*].total.confirm') ls_province_confirm = list(zip(ls_province_names, ls_confirm_vals,)) return ls_province_confirm, world_data #%% ls_province_cfm, dic_world_data = catch_cn_disease_dis() print(ls_province_cfm) #%% # 繪制全國疫情地圖 def map_cn_disease_dis() -> Map: c = ( Map() .add('中國', ls_province_cfm, 'china') .set_global_opts( title_opts=opts.TitleOpts(title='全國新型冠狀病毒疫情地圖(確診數)'), visualmap_opts=opts.VisualMapOpts(is_show=True, split_number=7, is_piecewise=True, # 是否為分段型 pos_top='center', pieces=[ {'min': 50000, 'color': '#7f1818'}, #不指定 max {'min': 2000, 'max': 4999}, {'min': 1000, 'max': 1999}, {'min': 500, 'max': 999}, {'min': 100, 'max': 499}, {'min': 10, 'max': 99}, {'min': 0, 'max': 9} ], ), ) ) return c # 獲取河南省確診分布數據 def catch_fj_disease_dis(): dic_world_data = catch_cn_disease_dis()[1] dic_fj_cfm = dict() # 解析河南各個城市的數據 dic_fj = jsonpath.jsonpath(dic_world_data, expr='$.areaTree[0].children[?(@.name=="'+shenname+'")].children[*]') for item in dic_fj: if item['name'] not in dic_fj_cfm: dic_fj_cfm.update({item['name']: 0}) dic_fj_cfm[item['name']] += int(item['total']['confirm']) return dic_fj_cfm dic_fj_cfm = catch_fj_disease_dis() # %% # 繪制河南省疫情地圖 def map_fj_disease_dis() -> Map: # dic_fj_cfm = catch_fj_disease_dis ls_fj_cities = [name + '市' for name in dic_fj_cfm.keys()] c = ( Map() .add(shenname, [list(z) for z in zip(ls_fj_cities, dic_fj_cfm.values())], shenname) .set_series_opts(label_opts=opts.LabelOpts(is_show=True, formatter='{b}\n{c}例')) .set_global_opts( title_opts=opts.TitleOpts(title=shenname+'新型冠狀病毒疫情地圖(確診數)'), visualmap_opts=opts.VisualMapOpts(is_show=True, split_number=5, is_piecewise=True, # 是否為分段型 pos_top='center', pieces=[ {'min': 100}, {'min': 50, 'max': 100}, {'min': 20, 'max': 49}, {'min': 10, 'max': 19}, {'min': 1, 'max': 9}, {'value': 0, "label": '無確診病例', "color": 'green'}], ), ) ) return c map_fj_disease_dis().render(shenname+'疫情地圖.html') map_cn_disease_dis().render("全國疫情地圖.html")
=========================修改=============
from pyquery import PyQuery as pq from bs4 import BeautifulSoup import datetime import numpy as np import re import requests import time, json, requests import jsonpath from pyecharts.charts import Map import pyecharts.options as opts #%% shenname="湖北" # 全國疫情地區分布(各省確診病例) def catch_cn_disease_dis(): timestamp = '%d'%int(time.time()*1000) url_area = ('https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5' '&callback=&_=') + timestamp world_data = json.loads(requests.get(url=url_area).json()['data']) china_data = jsonpath.jsonpath(world_data, expr='$.areaTree[0].children[*]') ls_province_names = jsonpath.jsonpath(china_data, expr='$[*].name') ls_confirm_vals = jsonpath.jsonpath(china_data, expr='$[*].total.confirm') ls_province_confirm = list(zip(ls_province_names, ls_confirm_vals,)) return ls_province_confirm, world_data #%% ls_province_cfm, dic_world_data = catch_cn_disease_dis() print(ls_province_cfm) #%% # 繪制全國疫情地圖 def map_cn_disease_dis() -> Map: c = ( Map() .add('中國', ls_province_cfm, 'china') .set_global_opts( title_opts=opts.TitleOpts(title='全國新型冠狀病毒疫情地圖(確診數)'), visualmap_opts=opts.VisualMapOpts(is_show=True, split_number=7, is_piecewise=True, # 是否為分段型 pos_top='center', pieces=[ {'min': 50000, 'color': '#7f1818'}, #不指定 max {'min': 2000, 'max': 4999}, {'min': 1000, 'max': 1999}, {'min': 500, 'max': 999}, {'min': 100, 'max': 499}, {'min': 10, 'max': 99}, {'min': 0, 'max': 9} ], ), ) ) return c # 獲取河南省確診分布數據 def catch_fj_disease_dis(): dic_world_data = catch_cn_disease_dis()[1] dic_fj_cfm = dict() # 解析河南各個城市的數據 dic_fj = jsonpath.jsonpath(dic_world_data, expr='$.areaTree[0].children[?(@.name=="'+shenname+'")].children[*]') for item in dic_fj: if item['name'] not in dic_fj_cfm: dic_fj_cfm.update({item['name']: 0}) dic_fj_cfm[item['name']] += int(item['total']['confirm']) return dic_fj_cfm dic_fj_cfm = catch_fj_disease_dis() # %% # 繪制河南省疫情地圖 def map_fj_disease_dis() -> Map: # dic_fj_cfm = catch_fj_disease_dis ls_fj_cities = [name + '市' for name in dic_fj_cfm.keys()] c = ( Map() .add(shenname, [list(z) for z in zip(ls_fj_cities, dic_fj_cfm.values())], shenname) .set_series_opts(label_opts=opts.LabelOpts(is_show=True, formatter='{b}\n{c}例')) .set_global_opts( title_opts=opts.TitleOpts(title=shenname+'新型冠狀病毒疫情地圖(確診數)'), visualmap_opts=opts.VisualMapOpts(is_show=True, split_number=7, is_piecewise=True, # 是否為分段型 pos_top='center', pieces=[ {'min': 5000}, {'min': 1000, 'max': 4999}, {'min': 100, 'max': 999}, {'min': 50, 'max': 99}, {'min': 20, 'max': 49}, {'min': 10, 'max': 19}, {'min': 1, 'max': 9}, {'value': 0, "label": '無確診病例', "color": 'green'}], ), ) ) return c map_fj_disease_dis().render(shenname+'疫情地圖.html') map_cn_disease_dis().render("全國疫情地圖.html")
