我试过这个,但我只给出了 1990 年的值:import requestdef get_world_bank_data(indicator_code): url = f\'https://api.worldbank.org/v2/country/DEU/indicator/{indicator_code}?format=
我尝试了这个,但我只给出了 1990 年的值:
import requests
def get_world_bank_data(indicator_code):
url = f"https://api.worldbank.org/v2/country/DEU/indicator/{indicator_code}?format=json"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if data and isinstance(data, list) and len(data) > 1:
return data[1] # The second element contains the data
return None
def get_latest_value(data):
for entry in data:
if entry['value'] is not None:
return entry['date'], entry['value']
return None, None
indicators = {
"GDP (current US$)": "NY.GDP.MKTP.CD",
"General government gross debt (% of GDP)": 'GC.DOD.TOTL.GD.ZS',
"Fiscal balance (% of GDP)": "GC.NLD.TOTL.GD.ZS",
#"Total external debt stocks (current US$)": "GC.DYN.MTOT.CD",
"Interest payments (% of revenue)": "GC.XPN.INTP.RV.ZS"
}
data = {}
for indicator_name, indicator_code in indicators.items():
indicator_data = get_world_bank_data(indicator_code)
date, value = get_latest_value(indicator_data)
if date and value is not None:
data[indicator_name] = {"year": date, "value": value}
else:
data[indicator_name] = {"year": None, "value": None}
for key, val in data.items():
print(f"{key} in {val['year']}: {val['value']}")
结果:
我想要获得当前或去年的数据。