Python獲取江蘇疫情實(shí)時(shí)數(shù)據(jù)及爬蟲(chóng)分析
1.引言
最近江蘇南京、湖南張家界陸續(xù)爆發(fā)疫情,目前已波及8省22市,全國(guó)共有2個(gè)高風(fēng)險(xiǎn)地區(qū),52個(gè)中風(fēng)險(xiǎn)地區(qū)。身在南京,作為兢兢業(yè)業(yè)的打工人,默默地成為了“蘇打綠”。為了關(guān)注疫情狀況,今天我們用python來(lái)爬一爬疫情的實(shí)時(shí)數(shù)據(jù)。
2.獲取目標(biāo)網(wǎng)站
為了使用python來(lái)獲取疫情數(shù)據(jù),我們需要找一個(gè)疫情實(shí)時(shí)追蹤數(shù)據(jù)發(fā)布網(wǎng)站,國(guó)內(nèi)比較有名的是騰訊新聞、網(wǎng)易新聞等,這些網(wǎng)站疫情內(nèi)容都大同小異,主要包括國(guó)內(nèi)疫情、海外疫情,每日新增確診趨勢(shì),疫苗接種情況等,這里我們選用騰訊新聞疫情發(fā)布頁(yè)來(lái)進(jìn)行數(shù)據(jù)爬取分析。

網(wǎng)站分析:
- 使用chrome瀏覽器 打開(kāi)疫情發(fā)布頁(yè)網(wǎng)址 ,如上圖所示
- 我們按F12 進(jìn)入開(kāi)發(fā)者模式,按 ctrl+R 刷新頁(yè)面
- 在Network下找到 getOnsInfo?name=disease_h5列,獲得爬取目標(biāo)網(wǎng)址

3.爬取目標(biāo)網(wǎng)站
我們寫(xiě)爬蟲(chóng)爬取網(wǎng)站數(shù)據(jù),需要安裝request庫(kù),安裝命令如下:
pip3 install requests
只需要三行代碼就可以獲取該網(wǎng)頁(yè)內(nèi)容,代碼如下:
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5' req = requests.get(url=url) content = json.loads(req.text)
打印爬去結(jié)果如下:

4.解析爬取內(nèi)容
上述網(wǎng)站內(nèi)容我們雖然爬取成功,接下來(lái)我們需要對(duì)爬取的結(jié)果進(jìn)行解析,從中找出我們感興趣的部分。
4.1. 解析全國(guó)今日總況

相應(yīng)的解析代碼如下:
def get_all_china(content):
tmp_data = content["data"]
area_data = json.loads(tmp_data)["areaTree"]
country = area_data[0]
country_list = []
name = country["name"]
today_confirm = country["today"]["confirm"]
now_confirm = country["total"]["nowConfirm"]
total_confirm = country["total"]["confirm"]
total_heal = country["total"]["heal"]
country_list.append([name, today_confirm, now_confirm, total_confirm, total_heal])
return country_list
打印結(jié)果如下:

輸出太丑了,這里使用PrettyTable庫(kù)對(duì)輸出進(jìn)行美化,代碼如下:
def format_list_prettytable(title,province_list):
table = PrettyTable(title)
for province in province_list:
table.add_row(province)
table.border = True
return table
結(jié)果如下:

4.2. 解析全國(guó)各省份疫情情況
依次類推,可解析全國(guó)各省市疫情情況,代碼如下:
def get_all_province(content):
tmp_data = content["data"]
area_data = json.loads(tmp_data)["areaTree"]
data = area_data[0]['children']
province_list = []
for province in data:
name = province["name"]
today_confirm = province["today"]["confirm"]
now_confirm = province["total"]["nowConfirm"]
total_confirm = province["total"]["confirm"]
total_heal = province["total"]["heal"]
province_list.append([name, today_confirm, now_confirm, total_confirm, total_heal])
return province_list
結(jié)果如下:

4.3. 解析江蘇各地級(jí)市疫情情況
最后,我們獲取江蘇省各地級(jí)市的疫情數(shù)據(jù),代碼如下:
def parse_jiangsu_province(content,key_province):
tmp_data = content["data"]
area_data = json.loads(tmp_data)["areaTree"]
data = area_data[0]['children']
city_list = []
for province in data:
name = province["name"]
if name == key_province:
children_list = province["children"]
for children in children_list:
city = children["name"]
today_new = children["today"]["confirm"]
now_confirm = children["total"]["nowConfirm"]
total_confirm = children["total"]["confirm"]
total_heal = children["total"]["heal"]
city_list.append([city, today_new, now_confirm, total_confirm, total_heal])
return city_list
結(jié)果如下:

5.結(jié)果可視化
使用matplotlib對(duì)上述爬去的江蘇各地級(jí)市疫情分布可視化,得到結(jié)果如下:
今日新增可視化結(jié)果如下:

現(xiàn)有確診可視化結(jié)果如下:

從上述圖表可以看出,今日疫情已擴(kuò)散至揚(yáng)州,揚(yáng)州今日新增感染人數(shù)最多,需引起重視。
6. 代碼
完整代碼
https://github.com/sgzqc/wechat/tree/main/20210731
7. 參考
到此這篇關(guān)于Python獲取江蘇疫情實(shí)時(shí)數(shù)據(jù)及爬蟲(chóng)分析的文章就介紹到這了,更多相關(guān)Python江蘇疫情內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 關(guān)于python爬蟲(chóng)應(yīng)用urllib庫(kù)作用分析
- python爬蟲(chóng)Scrapy框架:媒體管道原理學(xué)習(xí)分析
- python爬蟲(chóng)Mitmproxy安裝使用學(xué)習(xí)筆記
- Python爬蟲(chóng)和反爬技術(shù)過(guò)程詳解
- python爬蟲(chóng)之Appium爬取手機(jī)App數(shù)據(jù)及模擬用戶手勢(shì)
- 爬蟲(chóng)Python驗(yàn)證碼識(shí)別入門
- Python爬蟲(chóng)技術(shù)
- Python爬蟲(chóng)爬取商品失敗處理方法
- Python爬蟲(chóng)之Scrapy環(huán)境搭建案例教程
- Python爬蟲(chóng)中urllib3與urllib的區(qū)別是什么
- 教你如何利用python3爬蟲(chóng)爬取漫畫(huà)島-非人哉漫畫(huà)
- Python爬蟲(chóng)分析匯總
相關(guān)文章
python中startswith()和endswith()的用法詳解
Python startswith() 方法用于檢查字符串是否是以指定子字符串開(kāi)頭,endswith()方法主要是用于判斷字符串是否以指定字符或子字符串結(jié)尾,常用于判斷文件類型,對(duì)python startswith()和endswith()用法相關(guān)知識(shí)感興趣的朋友一起看看吧2021-10-10
Python面向?qū)ο罂偨Y(jié)及類與正則表達(dá)式詳解
Python中的類提供了面向?qū)ο缶幊痰乃谢竟δ埽侯惖睦^承機(jī)制允許多個(gè)基類,派生類可以覆蓋基類中的任何方法,方法中可以調(diào)用基類中的同名方法。這篇文章主要介紹了Python面向?qū)ο罂偨Y(jié)及類與正則表達(dá)式 ,需要的朋友可以參考下2019-04-04
解決更新tensorflow后應(yīng)用tensorboard報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了解決更新tensorflow后應(yīng)用tensorboard報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
解讀matplotlib和seaborn顏色圖(colormap)和調(diào)色板(color palette)
這篇文章主要介紹了matplotlib和seaborn顏色圖(colormap)和調(diào)色板(color palette),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06

