-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mark Cohen
committed
Jul 26, 2016
0 parents
commit e9340d4
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
selenium==2.53.6 |