Retrieve recent bills sponsored by a member of Congress

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.

Directions: Before executing the code below ...

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.

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.

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.

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.