Python获取2019-nCoV疫情实时追踪数据
戳藍字“CSDN云計算”關(guān)注我們哦!
來源 | 三行科創(chuàng)
責編 |?Carol
出品 | CSDN云計算(ID:CSDNcloud)?
2019-nCoV新型冠狀病毒引發(fā)的肺炎牽動全國人民的心,無數(shù)無畏的英雄兒女逆行而上奔赴前線,作者也于2020年2月2號回上海了開啟居家辦公模式。2月3日上午部門開了電話會議后,晚上刷手機刷這刷著覺得也要像那些醫(yī)務(wù)人員一樣勇敢,為疫情做點什么,于是有了這次2019-nCoV項目,本項目包括如下三部分。
第一部分 獲取疫情數(shù)據(jù)
第二部分 地理可視化
第三部分 病例數(shù)據(jù)規(guī)律探索
本篇主要講如何獲取實時疫情數(shù)據(jù)。
分析與操作
很多大型門戶網(wǎng)站和手機應(yīng)用都開設(shè)了專門的疫情實時追蹤數(shù)據(jù)網(wǎng)站和功能,比較友好的有騰訊新聞,網(wǎng)易新聞,新浪新聞,三者網(wǎng)頁感官大體差不多,有疫情最新總數(shù),疫情地圖,疫情病例曲線等,數(shù)據(jù)來源于國家及各地衛(wèi)生健康委員會每日發(fā)布的信息,今天我們就以騰訊新聞為例來獲取疫情實時數(shù)據(jù)。
具體操作
1、打開目標網(wǎng)站
2、按住F12,進入開發(fā)者模式,再按Ctrl+R刷新
3、在NetWork下找到getOnsInfo?name=disease_h5列
4, 雙擊該條目,跳轉(zhuǎn)到一個類似于https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=jQuery34106817348088859214_1580806762734&_=1580806762735網(wǎng)站,發(fā)現(xiàn)里面網(wǎng)站都是非常工整的json格式數(shù)據(jù),即我們想要獲取的疫情數(shù)據(jù),但是我們請求的url為https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5即可,因為后面callback參數(shù)只是記錄你訪問的一個標識。
完整代碼
疫情數(shù)據(jù)找到了,接下來就是通過requests模塊訪問并獲取并加以整理數(shù)據(jù)的事情了。
# -*- coding: utf-8 -*- """ Created on Tue Feb 4 10:27:51 2020 project name:2019-nCoV @author: 帥帥de三叔 """ import json, csv, requests #導(dǎo)入請求模塊 def get_data(): #定義獲取數(shù)據(jù)并寫入csv文件里的函數(shù)url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5" #請求網(wǎng)址response=requests.get(url).json() #發(fā)出請求并json化處理#print(response) #測試一下是否獲取數(shù)據(jù)了data = json.loads(response['data']) #提取數(shù)據(jù)部分#print(data.keys()) #獲取數(shù)據(jù)組成部分['chinaTotal', 'chinaAdd', 'lastUpdateTime', 'areaTree', 'chinaDayList', 'chinaDayAddList']update_time=data["lastUpdateTime"]chinaDayList=data["chinaDayList"] #歷史數(shù)據(jù) with open("每日病例.csv","w+",newline="") as csv_file:writer=csv.writer(csv_file)header=["date","confirm","suspect","dead","heal", "update_time"] #定義表頭writer.writerow(header)for i in range(len(chinaDayList)):data_row1=[chinaDayList[i]["date"],chinaDayList[i]["confirm"],chinaDayList[i]["suspect"],chinaDayList[i]["dead"],chinaDayList[i]["heal"], update_time]writer.writerow(data_row1) chinaDayAddList=data["chinaDayAddList"] #歷史新增數(shù)據(jù) with open("每日新增病例.csv","w+",newline="") as csv_file:writer=csv.writer(csv_file)header=["date","confirm","suspect","dead","heal","update_time"] #定義表頭writer.writerow(header)for i in range(len(chinaDayAddList)):data_row2=[chinaDayAddList[i]["date"],chinaDayAddList[i]["confirm"],chinaDayAddList[i]["suspect"],chinaDayAddList[i]["dead"],chinaDayAddList[i]["heal"], update_time]writer.writerow(data_row2)areaTree=data["areaTree"] #各地方數(shù)據(jù)with open("全國各城市病例數(shù)據(jù).csv","w+",newline="") as csv_file:writer=csv.writer(csv_file)header=["province", "city_name", "total_confirm", "total_suspect", "total_dead", "total_heal", "today_confirm", "today_suspect", "today_dead", "today_heal","update_time"]writer.writerow(header)china_data=areaTree[0]["children"] #中國數(shù)據(jù)for j in range(len(china_data)):province=china_data[j]["name"] #省份city_list=china_data[j]["children"] #該省份下面城市列表for k in range(len(city_list)): city_name=city_list[k]["name"] #城市名稱total_confirm=city_list[k]["total"]["confirm"] #總確認病例total_suspect=city_list[k]["total"]["suspect"] #總疑似病例total_dead=city_list[k]["total"]["dead"] #總死亡病例total_heal=city_list[k]["total"]["heal"] #總治愈病例today_confirm=city_list[k]["today"]["confirm"] #今日確認病例today_suspect=city_list[k]["total"]["suspect"] #今日疑似病例today_dead=city_list[k]["today"]["dead"] #今日死亡病例today_heal=city_list[k]["today"]["heal"] #今日治愈病例 print(province, city_name, total_confirm, total_suspect, total_dead, total_heal, today_confirm, today_suspect, today_dead, today_heal,update_time)data_row3=[province, city_name, total_confirm, total_suspect, total_dead, total_heal, today_confirm, today_suspect, today_dead, today_heal, update_time]writer.writerow(data_row3)if __name__=="__main__":get_data()代碼解讀
這里主要用到j(luò)son, csv, requests三個模塊,用requests發(fā)出請求,獲取返回的json格式數(shù)據(jù),我們可以用data.keys()打印出數(shù)據(jù)構(gòu)成,一共有’chinaTotal’, ‘chinaAdd’, ‘lastUpdateTime’, ‘a(chǎn)reaTree’, ‘chinaDayList’ 和 ‘chinaDayAddList’ 6部分數(shù)據(jù),分別對應(yīng)中國疫情總數(shù),當日新增疫情總數(shù),最近一次更新時間,各地方疫情明細,歷史疫情總數(shù)和歷史新增總數(shù),其中areaTree還包括海外數(shù)據(jù),在厘清數(shù)據(jù)組成之后就需要把各部分數(shù)據(jù)分別弄下來,對于’chinaTotal’, ‘chinaAdd’, ‘lastUpdateTime’這三部分數(shù)據(jù)都只有一條數(shù)據(jù),對后面的研究不大,故這次只準備獲取后面三部分’areaTree’, ‘chinaDayList’ 和 ‘chinaDayAddList’ ,采取了3次分別寫入csv文件中,并在每次寫入的時候增加了更新時間字段,使得字段完備。
數(shù)據(jù)截圖
后續(xù)
1、獲取數(shù)據(jù)后接下來工作是可視化了,有兩個方向,一個是pyecharts, 一個是basemap。
2、這里代碼每次獲取都是獲取三份數(shù)據(jù),一份是每日總病例,一份是每日新增病例,這兩份都是全國匯總數(shù)據(jù),第三份是全國各城市病例數(shù)據(jù),這個有明細,如果要看地圖變遷的話需要不同日期的數(shù)據(jù)。
3、在探索病例曲線特征的時候,可以簡單的按照[時間序列擬合出logistic模型,求解模型參數(shù),再對未來時日加以預(yù)測。
福利掃描添加小編微信,備注“姓名+公司職位”,入駐【CSDN博客】,加入【云計算學習交流群】,和志同道合的朋友們共同打卡學習! 推薦閱讀:GitHub 標星 14000+,阿里開源的 SEATA 如何應(yīng)用到極致? Serverless 終結(jié) Kubernetes? 享受夢幻技術(shù)盛宴,相約 Unite 2020 JavaScript 造就年薪超過 10 萬美元的開發(fā)者們! 只需1分鐘,這個網(wǎng)站用AI分離歌曲的人聲、伴奏和樂器聲 10 大趨勢帶你預(yù)見 DeFi 2020! 真香,朕在看了!總結(jié)
以上是生活随笔為你收集整理的Python获取2019-nCoV疫情实时追踪数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MongoDB+阿里云 最新数据库独家
- 下一篇: 雅士利牵手阿里云实现新零售改造,双11全