API data and Python
Today I will discuss how to retreive restful API data using python.
In particular we will make use of the following libraries:
requests,json and pandas all of the code in this post can be downloaded on my cloud anaconda page here.
First lets import our libraries, if you do not have these libraries, use the !pip install command to install them.
For this demo we will use a free API that is hosted at http://api.icndb.com. This particular API returns jokes about the famous western and kungfu guru Chuck Norris. At its core we can achieve a simple get request as follows :
This is pretty good in terms of basic functionality and concepts, but what if we only care about the joke itself? We can see it is nested inside the value portion of the return dictionary. For this we can enlist the help of the JSON library.
Here we get a more appealing result just the actual joke text returned from the API.
Lastly lets see if we can get this results into a pandas dataframe. I will write a for loop to call a GET request 5 times, parse the joke portion of the returned json data and append that to our dataframe.