好友
阅读权限 30
听众
最后登录 1970-1-1
本帖最后由 ciker_li 于 2020-2-1 22:09 编辑
最近疫情严重,在家无聊,每天看疫情通报时,只能看到全国的疫情地图,找不到本省的。闲着也是闲着,自己动手做个本省的疫情地图
话不多说,直接上代码吧
# -*- coding: utf-8 -*-
import re
import time
from fake_useragent import UserAgent
from pyecharts.charts import Map
from pyecharts import options as opts
import requests
import json
ua = UserAgent()
headers = {'User-Agent': ua.random}
url = "https://c.m.163.com/ug/api/wuhan/app/index/feiyan-data-list?t=1580469818264"
#爬取疫情数据
def geturl(url):
try:
response = requests.get(url,headers=headers)
print(response.status_code)
if response.status_code == 200:
content_field = json.loads(response.text)
list_datas_1 = content_field['data']['list']
return list_datas_1
else:
print('返回代码:'+response.status_code)
return None
except Exception as e:
print('此页有问题!',e)
return None
#制作疫情地图
def makemap(dict):
# 省和直辖市
province_distribution = dict
value=province_distribution.values()
# maptype='china' 只显示全国直辖市和省级
title = str(int(time.strftime("%Y%m%d"))-1)+"山西疫情地图"
map = Map()
map.set_global_opts(
title_opts=opts.TitleOpts(title=title),
visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True,
pieces=[
{"max": 30, "min": 16, "label": ">16", "color": "#780707"}, #数据范围分段,分颜色,可以根据数据大小具体分配大小
{"max": 15, "min": 11, "label": "15-11", "color": "#8A0808"},
{"max": 10, "min": 9, "label": "10-9", "color": "#B40404"},
{"max": 8, "min": 7, "label": "8-7", "color": "#CD1111"},
{"max": 6, "min": 5, "label": "6-5", "color": "#DF0101"},
{"max": 4, "min": 3, "label": "4-3", "color": "#F68181"},
{"max": 2, "min": 1, "label": "2-1", "color": "#F5A9A9"},
{"max": 0, "min": 0, "label": "0", "color": "#FFFFFF"},
], ) #最大数据范围,分段
)
map.add(title, data_pair=province_distribution.items(), maptype="山西", is_roam=True)
map.render('山西疫情地图.html')
#生成本省疫情列表
def makedict(list):
dict1 = {}
for item in list:
for k,v in item.items():
if( v == "山西"): #替换“山西”,可以查各省的数据
dict1[item['name']+'市'] = int(item["confirm"])
return dict1
if __name__ == '__main__':
list_data = geturl(url)
dict_data = makedict(list_data)
makemap(dict_data)
数据是网易新闻客户端的,没有再具体到县区的,只能做到各省了
由于本省具体到各市感染人数不多,所以分的比较细,可以看出点差别,要是湖北,就得更改颜色范围
免费评分
查看全部评分
发帖前要善用【论坛搜索 】 功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。