【python】爬取疫情資料並進行視覺化

沙diao網友發表於2020-09-24

【python】爬取疫情資料並進行視覺化
資料來源:https://news.qq.com/zt2020/page/feiyan.htm#/?nojump=1
最終呈現:
在這裡插入圖片描述
在這裡插入圖片描述
視訊資料:https://www.bilibili.com/video/BV1f54y127dd

'''
課題:爬取疫情資料 進行視覺化
第一步:爬蟲爬取疫情資料
第二步:實現資料的視覺化
'''

import time
import json
import requests
import pandas as pd

url = r'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)
html = requests.get(url)
html.json() #將json轉化成dict形式
data = json.loads(html.json()['data'])
china_data = data['areaTree'][0]['children']
data_set = []

for i in china_data:
    data_dict = {}
    data_dict['province'] = i['name']
    data_dict['nowConfirm'] = i['total']['nowConfirm']
    data_dict['confirm'] = i['total']['confirm']
    data_dict['dead'] = i['total']['dead']
    data_dict['heal'] = i['total']['heal']
    data_dict['deadRate'] = i['total']['deadRate']
    data_dict['healRate'] = i['total']['healRate']

    data_set.append(data_dict)


df = pd.DataFrame(data_set)#生成dataframe資料 (類似表格資料)
df.to_csv('epidemic_data.csv') #儲存到本地的csv資料

# 資料視覺化 matplotlib pyecharts(好看)

from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Pie, Grid, Map
df2 = df.sort_values(by = ['nowConfirm'], ascending=False)[:5]
print(df2)
#將dateframe轉換成list
df2 = [list(i) for i in zip(df2['province'].values.tolist(), df2['nowConfirm'].values.tolist())]

#畫餅圖並儲存html檔案至當前目錄
pie = (
    Pie()
    .add(
        "",
        df2,
        radius=["10%", "30%"]
    )
    .set_global_opts(
        legend_opts=opts.LegendOpts(orient="vertical", pos_top="70%", pos_left="70%"),
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)

pie.render()#儲存html檔案至當前目錄


china_map = (
    Map()
    .add("現有確診", df2)
    .set_global_opts(
        title_opts=opts.TitleOpts(title="各地區確診人數", pos_top="48%", pos_left="65%"),
        visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True),
        legend_opts=opts.LegendOpts(pos_left="90%", pos_top="60%")
    )
)
china_map.render('china_map.html')



相關文章