Skip to content

Instantly share code, notes, and snippets.

@benoit-cty
Created October 8, 2020 08:51
Show Gist options
  • Save benoit-cty/24cb41e4d5a99fc39a5ff109883d2b67 to your computer and use it in GitHub Desktop.
Save benoit-cty/24cb41e4d5a99fc39a5ff109883d2b67 to your computer and use it in GitHub Desktop.
Download WorkBank Data
import requests
import os
'''
https://climateknowledgeportal.worldbank.org/download-data
'''
PATH = '/temp/'
nature_of_data = ['projection', 'historical']
past_time_series = ["1901-1930", "1931-1960", "1961-1990", "1901-2016", "1991-2016"]
futu_time_series = ["2020_2039", "2040_2059", "2060_2079", "2080_2099"]
for nature in nature_of_data:
time_series=past_time_series if nature == 'historical' else futu_time_series
data_type = '' if nature == 'historical' else '/mavg'
projection = '' if nature == 'historical' else '/rcp85'
for period in time_series:
country_code = 'FRA'
country_name = 'France'
filename = '_'.join([nature, period, country_code, country_name]) + '.csv'
url = 'https://climateknowledgeportal.worldbank.org/api/data/get-download-data/' \
+ f'{nature}{data_type}/pr{projection}/{period}/{country_code}/{country_name}'
destination = os.path.join(PATH, filename)
print(url, '->', destination)
r = requests.get(url)
if r.status_code != 200:
print(f'ERROR HTTP : {r.status_code} for {url}')
continue
if len(r.content) < 1_000:
print(f'ERROR HTTP content too small : ', r.content)
continue
with open(destination, 'wb') as f:
f.write(r.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment