Tuesday, April 20, 2010

Accessing world bank api using python

World bank have recently release their data and you can access it through
http://data.worldbank.org/

It also includes API access through it. Which is a web api. Either way it is pretty easy to access from my 5 minute toying around. My experiment attempting to find population of malaysia,


import urllibimport urllib2
# here i use json, but the api also support xml
import json
# for url parameter, just to make it look niceparam = urllib.urlencode({'api_key':'your api key','format':'json'})
# get population of malaysia
url = 'http://open.worldbank.org/countries/my/indicators/SP.POP.TOTL'
data = urllib2.urlopen(url+'?'+ param)result = json.load(data)
# btw result[0] is the data on the pages etc, data start at index 1
print result[1]
This is the result, after i clean up my codes and clarify it

2 comments:

  1. Anonymous8:43 AM

    This code is very, very important to me. I am not a very experienced python programmer, so can you PLEASE clarify how to use it? where can I get an API key? which python version are you using? I keep getting data = urllib2.urlopen(url+'?'+ param)result = json.load(data)

    thanks

    ReplyDelete
  2. i am using python 2.6

    and you can do it to a shell....

    ReplyDelete