採坑Python製作全國疫情地圖

看了微信大牛用Python繪製全國疫情地圖,也手癢試一把。
基本步驟就是:
1.安裝環境
2.抓取數據
3.繪製地圖
4.輸出網頁

一、爬取數據
1)安裝常用的python爬蟲工具:beautifulsoup4、requests

pip install requests
pip install beautifulsoup4

2)找一個數據源
網址:https://news.qq.com/zt2020/page/feiyan.htm
這裏取到最終的接口getOnsInfo

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

3)python抓取數據

import requests
import json
from pyecharts.charts import Geo
from pyecharts import options as opts
from pyecharts.globals import GeoType,RenderType

url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
data = 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)pyecharts中的GEO來繪製地圖,首先需要安裝各種包。
我試了echarts-china-counties-pypkg和echarts-united-kingdom-pypkg 下載慢經常timeout,設了阿里雲鏡像也沒用。

pip install pyecharts
pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
pip install echarts-china-counties-pypkg
pip install echarts-china-misc-pypkg
pip 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),#不顯示Label
        effect_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")

四、全部源碼:

import requests
import json
from pyecharts.charts import Geo
from pyecharts import options as opts
from pyecharts.globals import GeoType,RenderType

url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
data = 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 = 10000
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),#不顯示Label
        effect_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")

五、效果
在這裏插入圖片描述
六、遇到的坑
1、個別包鏡像里拉不到,比如上面講的兩個包。
2、china_total = “確診:”+ data[‘chinaTotal’][‘confirm’] + \ " 疑似:" + data[‘chinaTotal’][‘suspect’] + \ " 死亡:" + data[‘chinaTotal’][‘dead’] + \ " 治癒:" + data[‘chinaTotal’][‘heal’] + \ " 更新日期:" + data['lastUpdateTime’]
字符串跟int類型數據拼接得用str()方法,不然報錯。
3、地圖放大縮小後,省的名稱不會隨着變(固定在初始化的位置)(這個還沒解決)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章