Extracting data from API
APIs¶
Instead of downloading World Bank data via a csv file, you're going to download the data using the World Bank APIs. The purpose of this exercise is to gain experience with another way of extracting data.
API is an acronym that stands for application programming interface. API’s provide a standardized way for two applications to talk to each other. In this case, the applications communicating with each other are the server application where World Bank stores data and your Jupyter notebook.
If you wanted to pull data directly from the World Bank’s server, you’d have to know what database system the World Bank was using. You’d also need permission to log in directly to the server, which would be a security risk for the World Bank. And if the World Bank ever migrated its data to a new system, you would have to rewrite all of your code again. The API allows you to execute code on the World Bank server without getting direct access.
Using APIs
In general, you access APIs via the web using a web address. Within the web address, you specify the data that you want. To know how to format the web address, you need to read an API's documentation. Some APIs also require that you send login credentials as part of your request. The World Bank APIs are public and do not require login credentials.
The Python requests library makes working with APIs relatively simple.
Example Indicators API
Run the code example below to request data from the World Bank Indicators API. According to the documntation, you format your request url like so:
http://api.worldbank.org/v2/countries/ + list of country abbreviations separated by ; + /indicators/ + indicator name + ? + options
where options can include
- per_page - number of records to return per page
- page - which page to return - eg if there are 5000 records and 100 records per page
- date - filter by dates
format - json or xml
and a few other options that you can read about here.
Comments
Post a Comment