I tried this, but I only gives a value from 1990:
import requestsdef 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 Nonedef get_latest_value(data): for entry in data: if entry['value'] is not None: return entry['date'], entry['value'] return None, Noneindicators = {"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']}")
Result:
- GDP (current US$) in 2023: 4456081016705.96
- General government gross debt (% of GDP) in 1990: 20.9305896078757
- Fiscal balance (% of GDP) in 2022: -2.99207337991803
- Interest payments (% of revenue) in 2022: 1.47665269641737
I would like to get the current or last year.