看了微信大牛用绘制全国疫情地图,也手痒试一把 。
基本步骤就是:
1.安装环境
2.抓取数据
3.绘制地图
4.输出网页
一、爬取数据
1)安装常用的爬虫工具:、
pip install requestspip install beautifulsoup4
2)找一个数据源
网址:
文章插图
这里取到最终的接口
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
3)抓取数据
import requestsimport jsonfrom pyecharts.charts import Geofrom pyecharts import options as optsfrom pyecharts.globals import GeoType,RenderTypeurl = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'data = http://www.kingceram.com/post/json.loads(requests.get(url=url).json()['data'])china = data['areaTree'][0]['children']data1 = []for i in range(len(china)):data1.append([china[i]['name'],china[i]['total']['confirm']])china_total = u" 确诊:" + str(data['chinaTotal']['confirm']) + \u" 疑似:" + str(data['chinaTotal']['suspect']) + \u" 死亡:" + str(data['chinaTotal']['dead']) +\u" 治愈:" + str(data['chinaTotal']['heal']) + \u" 更新日期:" + str(data['lastUpdateTime'])
二、绘制地图
1)中的GEO来绘制地图,首先需要安装各种包 。
我试了-china--pypkg和---pypkg 下载慢经常,设了阿里云镜像也没用 。
pip install pyechartspip install echarts-countries-pypkgpip install echarts-china-provinces-pypkgpip install echarts-china-cities-pypkgpip install echarts-china-counties-pypkgpip install echarts-china-misc-pypkgpip install echarts-united-kingdom-pypkg
2)绘制
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="yichart",data_pair=data1,type_=GeoType.EFFECT_SCATTER)#设置地图数据,动画方式为涟漪特效effect scatter.set_series_opts(#设置系列配置label_opts=opts.LabelOpts(is_show=False),#不显示Labeleffect_opts = opts.EffectOpts(scale = 5))#设置涟漪特效缩放比例.set_global_opts(#设置全局系列配置visualmap_opts=opts.VisualMapOpts(min_=0,max_=sum/len(data1),is_show=True),#设置视觉映像配置,最大值为平均值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.(path="./全国疫情图.html")
四、全部源码:
import requestsimport jsonfrom pyecharts.charts import Geofrom pyecharts import options as optsfrom pyecharts.globals import GeoType,RenderTypeurl = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'data = http://www.kingceram.com/post/json.loads(requests.get(url=url).json()['data'])china = data['areaTree'][0]['children']data1 = []for i in range(len(china)):data1.append([china[i]['name'],china[i]['total']['confirm']])china_total = u" 确诊:" + str(data['chinaTotal']['confirm']) + \u" 疑似:" + str(data['chinaTotal']['suspect']) + \u" 死亡:" + str(data['chinaTotal']['dead']) +\u" 治愈:" + str(data['chinaTotal']['heal']) + \u" 更新日期:" + str(data['lastUpdateTime'])sum = 10000geo = (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="yichart",data_pair=data1,type_=GeoType.EFFECT_SCATTER)#设置地图数据,动画方式为涟漪特效effect scatter.set_series_opts(#设置系列配置label_opts=opts.LabelOpts(is_show=False),#不显示Labeleffect_opts = opts.EffectOpts(scale = 5))#设置涟漪特效缩放比例.set_global_opts(#设置全局系列配置visualmap_opts=opts.VisualMapOpts(min_=0,max_=sum/len(data1),is_show=True),#设置视觉映像配置,最大值为平均值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="./全国疫情图.html")
- 松井大辅
- 翰金佰
- Arcmap软件-用渔网制作路网密度地图和铁路密度网地图-以广东省韶关市为例
- 转发有福利哦 Python实战 | 爬取北京海淀区一居室租房信息
- python 简单实现2048,一看就会
- 川航英雄机长怒了!猛批欧美著名空难纪录片:从未授权擅自制作 世界十大空难纪录片
- 凉糕的正确做法配方,凉糕的制作方法
- 凉粉制作过程,凉粉的制作方法
- chatgpt赋能Python-python3_10下载安装
- chatgpt赋能Python-pythongame怎么样