Python: Requesting and parsing data from API
One of my favorite things to-do in my spare time is to look at data. I really enjoy asking questions and trying to provide an answer by analyzing data. In this article I will talk about how I request data from propublica, opensecrets and the steps I took to parse the response.
After reading this article you will be able to
- Use requests to get data
- Parse a get request response
Requests library
It makes performing get requests very easy. I installed requests with pip and made the library available to the project with import.
- Make a get request to Propublica that returns all senators
I created a function called all_senators(), that goes to propublica and requests data about all senators who are currently serving. I created a variable to hold the url and created a dictionary to hold my api key. To make a get request I passed the url and headers and received a response object.