This script will:
See the API documentation at: https://projects.propublica.org/api-docs/congress-api/bills/
Running the script requires a free API code. Visit https://www.propublica.org/datastore/api/propublica-congress-api to request one.
To run a block of code, click the block, then press Shift/Enter. Generally, code blocks must be run in order, from the top of the notebook to the bottom, because later code blocks often depend upon things that took placew in earlier code blocks.
Script by Ken Blake, https://drkblake.com/
This code installs the requests
Python module. See: https://pypi.org/project/requests/. If you have never before installed the requests
module in your current Anaconda environment, the code will install it automatically. Installation is required only once per environment. If requests
is already installed, the output will tell you that the requirement to install requests
is already satisfied. Optional directions: If you do not want to run the installation every time you run the script, you may change pip install requests
to #pip install requests
.
import sys
!{sys.executable} -m pip install requests
Directions: Before executing the code below ...
Replace chamber
with either senate
or house
, and replace ST
with the capitalized two-letter postal abbrevition of a U.S. state, such as TN
for "Tennessee."
Substitute your API key for YourAPIKeyHere
. The code will not run without a valid API key.
url = 'https://api.propublica.org/congress/v1/members/chamber/ST/current.json'
headers = {'X-API-Key': 'YourAPIKeyHere'}
This code uses the requests
module to retrieve the data in JSON format from the API. It then saves the data in a variable r1
, translates the JSON data into a dictionary, stores the dictionary as memberdata
, and iterates through memberdata
, printing the name and ID of every current member of Congress for the state and chamber specified.
#Opening the requests module
import requests
#Fetching the data
r1 = requests.get(url, headers=headers)
memberdata = r1.json()
#Printing headers
print("Name |","ID |")
#Printing the data
for each_item in memberdata["results"]:
try:
print(
each_item["name"],"|",
each_item["id"],"|",
)
except:
continue
Directions: Before executing the code below, replace MemberID
with the ID number, from the list above, of the member whose most recently proposed bills you want to look up. The code retrieves the data in JSON format from the API and saves the data in a variable r2
, translates the JSON data into a dictionary, stores the dictionary as billdata
, then iterates through billdata
, printing the ID, introduction date, GovTrack URL, and primary subject of each bill.
#Fetching the data
url = 'https://api.propublica.org/congress/v1/members/MemberID/bills/introduced.json'
r2 = requests.get(url, headers=headers)
billdata = r2.json()
#Printing headers
print("ID |","Intro Date |","Govtrack URL |","Primary subject |")
#Printing the data
for each_item in billdata["results"][0]["bills"]:
try:
print(
each_item["number"],"|",
each_item["introduced_date"],"|",
each_item["govtrack_url"],"|",
each_item["primary_subject"],"|",
)
except:
continue
This code iterates through the billdata
dictionary a second time, this time printing the ID, latest major action, and latest major action date for each bill.
#Printing headers
print("ID |","Latest action |","Date |")
#Printing data
for each_item in billdata["results"][0]["bills"]:
try:
print(
each_item["number"],"|",
each_item["latest_major_action"],"|",
each_item["latest_major_action_date"],"|",
)
except:
continue
Directions: Change BillData.txt
to whatever file name you want your data to be stored in. This code iterates through the billdata
dictionary a third time, this time writing each bill's sponsor name, ID number, introduced date, GovTrack URL, primary subject, latest major action, and latest major action date to a locally saved text file with "|" characters as field delimiters. Running the code without changing the filename will append the new results to the content of the original file. The resulting file can be imported into a spreadsheet program by specifying | characters as delimiters.
#Saving data as a delimited file, with | as the delimiter
for each_item in billdata["results"][0]["bills"]:
try:
fout = open('BillData.txt', 'at')
print(
each_item["sponsor_name"],"|",
each_item["number"],"|",
each_item["introduced_date"],"|",
each_item["govtrack_url"],"|",
each_item["primary_subject"],"|",
each_item["latest_major_action"],"|",
each_item["latest_major_action_date"],"|",
file=fout)
fout.close()
except:
continue