Python獲取江蘇疫情實時數(shù)據(jù)及爬蟲分析
1.引言
最近江蘇南京、湖南張家界陸續(xù)爆發(fā)疫情,目前已波及8省22市,全國共有2個高風(fēng)險地區(qū),52個中風(fēng)險地區(qū)。身在南京,作為兢兢業(yè)業(yè)的打工人,默默地成為了“蘇打綠”。為了關(guān)注疫情狀況,今天我們用python來爬一爬疫情的實時數(shù)據(jù)。
2.獲取目標(biāo)網(wǎng)站
為了使用python來獲取疫情數(shù)據(jù),我們需要找一個疫情實時追蹤數(shù)據(jù)發(fā)布網(wǎng)站,國內(nèi)比較有名的是騰訊新聞、網(wǎng)易新聞等,這些網(wǎng)站疫情內(nèi)容都大同小異,主要包括國內(nèi)疫情、海外疫情,每日新增確診趨勢,疫苗接種情況等,這里我們選用騰訊新聞疫情發(fā)布頁來進(jìn)行數(shù)據(jù)爬取分析。
網(wǎng)站分析:
- 使用chrome瀏覽器 打開疫情發(fā)布頁網(wǎng)址 ,如上圖所示
- 我們按F12 進(jìn)入開發(fā)者模式,按 ctrl+R 刷新頁面
- 在Network下找到 getOnsInfo?name=disease_h5列,獲得爬取目標(biāo)網(wǎng)址
3.爬取目標(biāo)網(wǎng)站
我們寫爬蟲爬取網(wǎng)站數(shù)據(jù),需要安裝request庫,安裝命令如下:
pip3 install requests
只需要三行代碼就可以獲取該網(wǎng)頁內(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)容我們雖然爬取成功,接下來我們需要對爬取的結(jié)果進(jìn)行解析,從中找出我們感興趣的部分。
4.1. 解析全國今日總況
相應(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庫對輸出進(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. 解析全國各省份疫情情況
依次類推,可解析全國各省市疫情情況,代碼如下:
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. 解析江蘇各地級市疫情情況
最后,我們獲取江蘇省各地級市的疫情數(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對上述爬去的江蘇各地級市疫情分布可視化,得到結(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ù)據(jù)及爬蟲分析的文章就介紹到這了,更多相關(guān)Python江蘇疫情內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。