最近需要人均預(yù)期壽命沛膳、人均健康預(yù)期壽命等衛(wèi)生健康方面的數(shù)據(jù)缨称,就去世界衛(wèi)生組織(WHO)找了找,發(fā)現(xiàn)里面相關(guān)的數(shù)據(jù)還挺多的汇在,而且還有數(shù)據(jù)APIwho.int/data/gho/info/gho-odata-api]铡恕,里面一共可以找到2000多個(gè)全球各國(guó)的關(guān)于健康方面的數(shù)據(jù)琢感,比如預(yù)期壽命、孕產(chǎn)婦死亡數(shù)等探熔。
這樣的話驹针,可以用python批量獲取WHO官網(wǎng)的數(shù)據(jù),之后想用的話可以從里面找了诀艰。
首先柬甥,先獲取關(guān)于指標(biāo)信息的數(shù)據(jù)[ghoapi.azureedge.net/api/Indicator],這個(gè)數(shù)據(jù)里有所有指標(biāo)名(IndicatorName)以及對(duì)應(yīng)的指標(biāo)編碼(IndicatorCode)其垄,而這個(gè)code就是用于構(gòu)造獲取數(shù)據(jù)鏈接url的苛蒲。
每個(gè)指標(biāo)的下載鏈接url都是 https://ghoapi.azureedge.net/api/ + IndicatorCode
以出生時(shí)預(yù)期壽命
(Life expectancy at birth (years))這個(gè)指標(biāo)為例,它對(duì)應(yīng)的IndicatorCode是WHOSIS_000001
绿满,下載鏈接就是https://ghoapi.azureedge.net/api/WHOSIS_000001
臂外,將該鏈接復(fù)制到瀏覽器,就可以看到這個(gè)指標(biāo)的相關(guān)數(shù)據(jù)了喇颁。
這個(gè)數(shù)據(jù)格式是Json格式漏健,肉眼不大好看,可以用python轉(zhuǎn)為數(shù)據(jù)框并導(dǎo)出excel橘霎。
好了蔫浆,以下是具體過(guò)程的代碼。
1. 獲取Indicators和DimensionValues文件
這兩個(gè)文件都是信息文件姐叁。
Indicators文件告訴你有哪些指標(biāo)瓦盛,DimensionValues文件告訴你之后的每個(gè)指標(biāo)中涉及到的一些維度(Dimension)洗显,比如,國(guó)家縮寫(xiě)分別對(duì)應(yīng)什么國(guó)家谭溉,可以算是個(gè)值標(biāo)簽文件吧墙懂。
import requests
import pandas as pd
# 獲取數(shù)據(jù)
def request_data(url):
req = requests.get(url, timeout = 30) # 請(qǐng)求連接
req_jason = req.json() # 獲取數(shù)據(jù)
return req_jason
# 轉(zhuǎn)為df
def to_df(url):
data = request_data(url)
value = data['value']
df = pd.DataFrame(value)
return df
# df導(dǎo)出excel
def to_excel(url, fname):
df = to_df(url)
df.to_excel(fname, index = 0)
################################################獲取 Indicators
url = "https://ghoapi.azureedge.net/api/Indicator"
fname = 'WHO_Indicators.xlsx'
to_excel(url, fname)
###################################################### 獲取指標(biāo)值
url = "https://ghoapi.azureedge.net/api/DIMENSION/COUNTRY/DimensionValues"
fname = "DimensionValues.xlsx"
to_excel(url,fname)
導(dǎo)出的WHO_Indicators.xlsx的部分截圖:
英文指標(biāo)名看起來(lái)有些費(fèi)勁,所以扮念,就用百度翻譯API批量翻譯了一下指標(biāo)名,這樣看起來(lái)不用那么費(fèi)勁~
導(dǎo)出的DimensionValues.xlsx的部分截圖:
2. 所有指標(biāo)的數(shù)據(jù)
正如上面所說(shuō)碧库,根據(jù)Indicators文件里的IndicatorCode下載每個(gè)指標(biāo)數(shù)據(jù)柜与,并以指標(biāo)名作為文件名導(dǎo)出excel。
import requests
import pandas as pd
import time,re,random
# WHO
# 獲取數(shù)據(jù)
def request_data(url):
headers = {'User-Agent':"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3"}
# 嘗試
attempts = 0
success = False
while attempts < 10 and not success:
try:
req = requests.get(url, timeout = 30, headers = headers) # 請(qǐng)求連接
success = True
except:
attempts += 1
if attempts == 10:
break
req_jason = req.json() # 獲取數(shù)據(jù)
# time.sleep(random.random()*2)
return req_jason
# 轉(zhuǎn)為df
def to_df(url):
data = request_data(url)
value = data['value']
df = pd.DataFrame(value)
return df
# df導(dǎo)出excel
def to_excel(url, fname):
df = to_df(url)
df.to_excel(fname, index = 0)
######################################################## 根據(jù)Indicators將所有數(shù)據(jù)下載下來(lái)
url = "https://ghoapi.azureedge.net/api/Indicator"
df = to_df(url)
# 生成序號(hào)嵌灰,便于后續(xù)對(duì)應(yīng)excel
df['SequenceNumber'] = range(len(df))
for i in range(len(df)):
dataurl = "https://ghoapi.azureedge.net/api/" + df['IndicatorCode'][i]
IndicatorName = re.sub(r'[(](.*)[)]','',df['IndicatorName'][i])
IndicatorName = IndicatorName.replace('_','').replace(' ','_').replace("'","").replace(">","").replace("<","").replace(":","").replace("/","")
fname = str(df['SequenceNumber'][i]) + '_' + IndicatorName + '.xlsx'
to_excel(dataurl, fname)
print(fname, '已保存', '進(jìn)度:{:.2%}'.format(i/len(df)))
導(dǎo)出的數(shù)據(jù)文件部分截圖:
GZ號(hào):amazingdata (數(shù)據(jù)格子鋪)
后臺(tái)回復(fù):WHO弄匕,可獲取這2000多個(gè)指標(biāo)數(shù)據(jù)excel文件的下載鏈接。