最近需要人均預(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多個全球各國的關(guān)于健康方面的數(shù)據(jù),比如預(yù)期壽命、孕產(chǎn)婦死亡數(shù)等。
這樣的話,可以用python批量獲取WHO官網(wǎng)的數(shù)據(jù),之后想用的話可以從里面找了。
首先,先獲取關(guān)于指標信息的數(shù)據(jù)[ghoapi.azureedge.net/api/Indicator],這個數(shù)據(jù)里有所有指標名(IndicatorName)以及對應(yīng)的指標編碼(IndicatorCode),而這個code就是用于構(gòu)造獲取數(shù)據(jù)鏈接url的。
每個指標的下載鏈接url都是 https://ghoapi.azureedge.net/api/ + IndicatorCode
以出生時預(yù)期壽命(Life expectancy at birth (years))這個指標為例,它對應(yīng)的IndicatorCode是WHOSIS_000001,下載鏈接就是https://ghoapi.azureedge.net/api/WHOSIS_000001,將該鏈接復(fù)制到瀏覽器,就可以看到這個指標的相關(guān)數(shù)據(jù)了。

這個數(shù)據(jù)格式是Json格式,肉眼不大好看,可以用python轉(zhuǎn)為數(shù)據(jù)框并導(dǎo)出excel。
好了,以下是具體過程的代碼。
1. 獲取Indicators和DimensionValues文件
這兩個文件都是信息文件。
Indicators文件告訴你有哪些指標,DimensionValues文件告訴你之后的每個指標中涉及到的一些維度(Dimension),比如,國家縮寫分別對應(yīng)什么國家,可以算是個值標簽文件吧。
import requests
import pandas as pd
# 獲取數(shù)據(jù)
def request_data(url):
req = requests.get(url, timeout = 30) # 請求連接
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)
###################################################### 獲取指標值
url = "https://ghoapi.azureedge.net/api/DIMENSION/COUNTRY/DimensionValues"
fname = "DimensionValues.xlsx"
to_excel(url,fname)
導(dǎo)出的WHO_Indicators.xlsx的部分截圖:
英文指標名看起來有些費勁,所以,就用百度翻譯API批量翻譯了一下指標名,這樣看起來不用那么費勁~

導(dǎo)出的DimensionValues.xlsx的部分截圖:

2. 所有指標的數(shù)據(jù)
正如上面所說,根據(jù)Indicators文件里的IndicatorCode下載每個指標數(shù)據(jù),并以指標名作為文件名導(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) # 請求連接
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ù)下載下來
url = "https://ghoapi.azureedge.net/api/Indicator"
df = to_df(url)
# 生成序號,便于后續(xù)對應(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, '已保存', '進度:{:.2%}'.format(i/len(df)))
導(dǎo)出的數(shù)據(jù)文件部分截圖:

GZ號:amazingdata (數(shù)據(jù)格子鋪)
后臺回復(fù):WHO,可獲取這2000多個指標數(shù)據(jù)excel文件的下載鏈接。