24
loading...
This website collects cookies to deliver better user experience
import requests
import pandas as pd
key
. In most instances you can create an API request with just a couple lines of code.key = 'some_key'
url = f'https://api.open.fec.gov/v1/candidates/?api_key={key}&page=1&sort_hide_null=false&sort_null_only=false&sort_nulls_last=false&per_page=100&sort=name'
r = requests.get(url).json()
.json()
method. Make sure to read the documentation for the API you're using. It will describe how the data will returned to you. for
loop to retreive the first 500 results (100 per page, which you can see is a parameter in the url string).num
and performs the basic GET request from earlier, substituting the page number in the url string with the parameter that's passed into the api_call function. Below is what is returned when we make the GET request.{
"api_version": "1.0",
"pagination": {
"per_page": 100,
"count": 44523,
"pages": 2227,
"page": 1
},
"results": [
{
"state": "US",
"candidate_status": "N",
"last_file_date": "2021-05-05",
"federal_funds_flag": false,
"load_date": "2021-07-27T06:29:31+00:00",
"party": "PAF",
"cycles": [
2022
],
results
key, so we will store that in a DataFrame object. This is where Pandas comes into play. Power BI works best with Pandas' DataFrames. Within the api_call function, we store each request in a variable df
and return that variable..concat()
method and pass in the frames list. Using the .concat()
method without an axis
kwarg will append the DataFrames to one another.df_final.info()
to see the results:pagination
key in the oringial result and extract the count of total pages in the request. Use that as the top end of your for loop to retreive ALL the data.