Skip to content

Commit

Permalink
initial version of the bond scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Cohen committed Jul 26, 2016
0 parents commit e9340d4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
== Bond Value Scraper

I wish there was a way to bulk upload a spreadsheet of bond serial numbers to https://www.treasurydirect.gov/BC/SBCPrice. There isn't. So I wrote a python script with Selenium Webdriver to parse a CSV containing the info needed to fill out the form and run it automatically.

Then, you just need to click save and copy and paste the table back into a spreadsheet for fiddling.

== Run
Use Python 3.2+
pip install -r requirements.txt
copy your bonds.csv file to the root. don't add it to git, please. I don't want to know
python bond_scraper.py
48 changes: 48 additions & 0 deletions bond_scraper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import csv
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.support.ui import Select

def format_date(issue_date):
issue_date_as_date = datetime.strptime(issue_date, "%b-%y")
return issue_date_as_date.strftime("%m/%y").strip()


def format_currency(denom):
denom = denom.replace("$", "")
denom = denom.replace(",", "")
return denom.strip()

"""
Create a CSV with the headers "SerialNumber", "IssueDate", "Denom"
"""
def run(bond_file, redemption_date):
with open(bond_file, 'r', encoding='utf-8') as bonds_csv:
bonds_reader = csv.DictReader(bonds_csv)
browser = webdriver.Firefox()
browser.get("https://www.treasurydirect.gov/BC/SBCPrice")
browser.implicitly_wait(3)

for idx, row in enumerate(bonds_reader):
serial_number = row["SerialNumber"]
issue_date = row["IssueDate"]
denom = row["Denom"]
print(row)

if(len(serial_number.strip()) > 0):
# does the $ need to be replaced by %?
browser.find_element_by_name("SBCForm")
browser.find_element_by_name("SerialNumber").clear()
browser.find_element_by_name("SerialNumber").send_keys(serial_number)
browser.find_element_by_name("IssueDate").clear()
browser.find_element_by_name("IssueDate").send_keys(format_date(issue_date))
browser.find_element_by_name("RedemptionDate").clear()
browser.find_element_by_name("RedemptionDate").send_keys(redemption_date)
Select(browser.find_element_by_name("Denomination")).select_by_value(format_currency(denom))
browser.find_element_by_name("btnAdd.x").click()

if __name__ == '__main__':
bond_file = "bonds.csv"
redemption_date = "11/2016"
run(bond_file, redemption_date)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
selenium==2.53.6

0 comments on commit e9340d4

Please sign in to comment.