python疫情爆發至2020年5月全球疫情數據可視化


一、選題背景

從疫情的爆發大家到現在,大家每天都在關注這疫情的相關情況,對此我想對這次疫情情況進行一次分析,讓大家更直觀的看到從疫情爆發到2020年5月份的全球疫情相關情況。了解中國以至全球的疫情情況。

二、爬蟲方案設計

1. 爬蟲名稱

全球疫情數據可視化

2、數據來源

目標網站是騰訊新聞

2. 爬取內容與數據特征分析

一、分析網站  

二、發送網絡請求並獲取數據 

三、將每個國家的疫情數據提取出來,包括國家名稱、確診人數、死亡人數、報告日期等等,保存在數組

四、打包數組並繪制圖片

三、網站頁面結構分析

 通過瀏覽器“審查元素”查看源代碼如下圖所示:

<!DOCTYPE html>
<html>
    <head> 
        <meta charset="utf-8"> 
        <title>疫情可視化系統</title> 
    </head>
    <body>
        <p><h2 style="color:#fff000;">--------基於Python爬蟲的疫情數據可視化系統(xx)--------</h2></p>
        <table width="820" border="0">
            <tr>
                <td style="background-color:#eeeeee;width:250px;width:400px;vertical-align:top;">
                    <b>全球疫情地圖</b> 
                    <a href="http://ci.jinmensuyin.com/user/201712300020/files/20200531/world1.html?_xsrf=2%7C55b64f9e%7C02ba08c5ec7be9f551b77d0531315ec8%7C1590286606" >
                    <img border="0" src="http://ci.jinmensuyin.com/user/201712300020/files/20200531/world1.png?_xsrf=2%7C55b64f9e%7C02ba08c5ec7be9f551b77d0531315ec8%7C1590286606" alt="世界地圖1" width="400" height="230"></a>
                </td>

                <td style="background-color:#eeeeee;height:250px;width:400px;vertical-align:top;">
                    <b>全球疫情柱狀圖</b> 
                    <a href="http://ci.jinmensuyin.com/user/201712300020/files/20200531/world2.html?_xsrf=2%7C55b64f9e%7C02ba08c5ec7be9f551b77d0531315ec8%7C1590286606" >
                    <img border="0" src="http://ci.jinmensuyin.com/user/201712300020/files/20200531/world2.png?_xsrf=2%7C55b64f9e%7C02ba08c5ec7be9f551b77d0531315ec8%7C1590286606" alt="世界地圖2" width="400" height="230"></a>
                </td>
            </tr>

            <tr>
                <td style="background-color:#eeeeee;width:250px;width:400px;vertical-align:bottom;">
                    <b>中國疫情地圖</b> 
                    <a href="http://ci.jinmensuyin.com/user/201712300020/files/20200531/China1.html?_xsrf=2%7C55b64f9e%7C02ba08c5ec7be9f551b77d0531315ec8%7C1590286606">
                    <img border="0" src="http://ci.jinmensuyin.com/user/201712300020/files/20200531/China1.png?_xsrf=2%7C55b64f9e%7C02ba08c5ec7be9f551b77d0531315ec8%7C1590286606" alt="中國地圖1" width="400" height="230"></a>
                </td>

                <td style="background-color:#eeeeee;height:250px;width:400px;vertical-align:bottom;">
                    <b>中國疫情餅狀圖</b> 
                    <a href="http://ci.jinmensuyin.com/user/201712300020/files/20200531/China2.html?_xsrf=2%7C55b64f9e%7C02ba08c5ec7be9f551b77d0531315ec8%7C1590286606">
                    <img border="0" src="http://ci.jinmensuyin.com/user/201712300020/files/20200531/China2.png?_xsrf=2%7C55b64f9e%7C02ba08c5ec7be9f551b77d0531315ec8%7C1590286606" alt="中國地圖2" width="400" height="230"></a>
                </td>
            </tr>


        </table>
    </body>
</html>

 

四、程序設計

1.導入python庫

 1 import requests
 2 import jsonpath 3 import json 4 5 import pyecharts 6 from pyecharts.charts import Map 7 from pyecharts import options as opts 8 9 # from countries_ch_to_en import countries_dict 10 11 from pyecharts.globals import CurrentConfig, NotebookType 12 CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB 13 14 # Bar - Bar_rotate_xaxis_label 15 # http://gallery.pyecharts.org/#/Bar/bar_rotate_xaxis_label

2.目標網站 來自騰訊新聞

1 #url='https://api.inews.qq.com/newsqa/v1/automation/foreign/country/ranklist'
2 url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'

3.發送網絡請求

1 resp=requests.get(url)
2 #print("data=",resp.text)

4.提取數據 類型轉換 json-->dict

data=json.loads(resp.text)
# print("data=",data)
data = json.loads(data['data'])

date = jsonpath.jsonpath(data,"$..lastUpdateTime") #時間
#print("date:",date) 

#print("data=",data)
data = data['areaTree']
#print("data=",data)
#data = data['children']
#print("data=",data)
    • 中國數據原始:
    • 中國省份數據:

5.將每個國家的疫情數據提取出來,包括國家名稱、確診人數、死亡人數、報告日期等等,保存在數組

name = jsonpath.jsonpath(data,"$[*].children[*].name")
confirm = jsonpath.jsonpath(data,"$[*].children[*].total..confirm")
dead = jsonpath.jsonpath(data,"$[*].children[*].total..dead")

print("data1=",name)
print("data2=",confirm)
print("data3=",dead)
print("data4=",date)
#print("date_Min,date_Max=",min(date),max(date))
#date_Max=max(date)

6.將數據按照繪圖模塊的格式要求將數組打包

data_zip = zip(name,confirm)
data_list=list(data_zip)
print("data3=",data_list)
#時間數據特殊處理一下
#date_string="2020."+str(date_Max)

7.使用pyechart模塊將疫情數據繪制在世界地圖上

# 生成一個Map類的對象map
map = Map()     
#向map對象添加數據
map.add(str(date)+" 全國疫情數據分布", data_list, maptype="china")

# 設置顯示的參數,連續型顯示
map.set_global_opts(title_opts=opts.TitleOpts(title="全國確診病例數據"), visualmap_opts=opts.VisualMapOpts(max_=1000))   
map.set_series_opts(label_opts=opts.LabelOpts(is_show=False))

8.繪圖

# 疫情數據生成html網頁格式呈現
map.render("China1.html")         
# 疫情數據生成jupyter notebook內部的網頁格式呈現

map.load_javascript()
#map.render_notebook()
# wolrd_data()
map.render_notebook()
  • 繪圖結果:

全國疫情餅狀圖

nowConfirm = jsonpath.jsonpath(data,"$[*].children[*].total..nowConfirm")

# 將數據按照繪圖模塊的格式要求將數組打包
data5_zip = zip(name,nowConfirm)
data5_list=list(data5_zip)

print("data5=",data5_list[0:5])

from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType

data5_list.sort(reverse=True,key=lambda x:x[1]) 

pie = Pie(init_opts = opts.InitOpts())
pie.add(str(date)+"全國現存確診疫情餅狀圖", data5_list[0:5])
pie.render("China2.html") 
pie.load_javascript()
pie.render_notebook()
  • 繪圖結果:

世界疫情-世界疫情圖

1.目標網站 來自騰訊新聞

url='https://api.inews.qq.com/newsqa/v1/automation/foreign/country/ranklist'

2.發送網絡請求

resp=requests.get(url)
#print("data=",resp.text)

3.提取數據 類型轉換 json-->dict

data=json.loads(resp.text)
#print("data=",data)
    • 世界地圖原始:

4.將每個國家的疫情數據提取出來,包括國家名稱、確診人數、死亡人數、報告日期等等,保存在數組

name = jsonpath.jsonpath(data,"$..name")
confirm = jsonpath.jsonpath(data,"$..confirm")
dead = jsonpath.jsonpath(data,"$..dead")
date = jsonpath.jsonpath(data,"$..date")
newConfirm = jsonpath.jsonpath(data,"$..confirmAdd")
#print("data1=",name)
#print("data2=",confirm)

print("date_Min,date_Max=",min(date),max(date))
date_Max=max(date)

5.補充中國的數據

# 2.目標網站  來自騰訊新聞
#url='https://api.inews.qq.com/newsqa/v1/automation/foreign/country/ranklist'
url1 = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
# 3.發送網絡請求
resp1=requests.get(url1)
#print("data=",resp.text)
# 4.提取數據   類型轉換 json-->dict
datac=json.loads(resp1.text)

datac = json.loads(datac['data'])
datec = jsonpath.jsonpath(datac,"$..lastUpdateTime") #時間
#print("data=",datac)

cConfirm = jsonpath.jsonpath(datac,"$..chinaTotal..confirm")
cDead = jsonpath.jsonpath(datac,"$..chinaTotal..dead")
cnewConfirm = jsonpath.jsonpath(datac,"$..chinaAdd..confirm")
cnewDead = jsonpath.jsonpath(datac,"$..chinaAdd..confirm")
print("cConfirm:",cConfirm)
print("cnewConfirm:",cnewConfirm)
print("cDead:",cDead)

6.將數據按照繪圖模塊的格式要求將數組打包

data_zip = zip(name,confirm)
data_list=list(data_zip)

data_list.append(('中國', cConfirm))
#print(data_list)
#print("data3=",data_list)
    
#時間數據特殊處理一下
date_string="2020."+str(date_Max)

7.使用pyechart模塊將疫情數據繪制在世界地圖上

# 生成一個Map類的對象map
map = Map()     
#向map對象添加數據
map.add(date_string+" 全球疫情數據分布", data_list, maptype="world", name_map=countries_dict)

# 設置顯示的參數,連續型顯示
map.set_global_opts(title_opts=opts.TitleOpts(title="全球全部確診病例數據"), visualmap_opts=opts.VisualMapOpts(max_=140000))   
map.set_series_opts(label_opts=opts.LabelOpts(is_show=False))

8.繪圖

# 疫情數據生成html網頁格式呈現
map.render("world1.html")         
# 疫情數據生成jupyter notebook內部的網頁格式呈現
map.load_javascript()
# wolrd_data() 
map.render_notebook()
  • 繪圖結果:

全球新增確診和死亡人數柱狀圖

data3_list=list(zip(name,newConfirm,dead))
data3_list.append(('中國', cnewConfirm[0],cDead[0]))
print(data3_list[-5:])
data3_list.sort(reverse=True,key=lambda x:x[1])
from pyecharts.charts import Bar
bar=Bar()
number=15
countrys=[]
values=[]
deads=[]
MaxValue=0
# for i in range(len(all_data)):
for i in range(number):
u = data3_list[i]
countrys.append(u[0])
values.append(u[1])
deads.append(u[2])
bar.add_xaxis(countrys)
bar.add_yaxis("新增確診", values)
bar.add_yaxis("死亡", deads)
bar.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-60)),
title_opts=opts.TitleOpts(title=date_string+"全球疫情前"+str(number)+"個國家", subtitle="新增確診和死亡病例"),
)
bar.render("world2.html")
bar.render_notebook()
  • 繪圖結果:

五、總結

  本文分析了中國從疫情爆發到2020年5月31號的疫情相關情況和全球從疫情爆發到2020年5月31號的疫情相關情況,通過疫情期間確診人數、治愈人數以及死亡人數的變化情況及趨勢,可以發現中國疫情爆發之前到2020年5月的全國疫情情況和疫情嚴重的省份的疫情相關情況。還分析了全球從疫情爆發到2020年5月31號的全球新增確診和死亡人數。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM