Update 12-May-2011, I cleaned up the code, added logging of the data to a tab-delimited file, and published it to github. Happy Hacking!
Someone suggested I create a bot that tweets the US National Debt. Here’s how I’m retrieving the National Debt amount from the US Treasury site. I then retrieve the US Population from census.gov to figure out each person’s share of the national debt.
I present to you @usadebtlevel, my debt-tweeting bot.
from BeautifulSoup import BeautifulSoup
import urllib2
debt_url = 'http://www.treasurydirect.gov/NP/BPDLogin?application=np'
page = urllib2.urlopen(debt_url)
soup = BeautifulSoup(page)
national_debt = soup.find('table',{'class':'data1'}).findAll('td')[3].text
as_of = soup.find('table',{'class':'data1'}).findAll('td')[0].text
population_url = 'http://www.census.gov/main/www/popclock.html'
population_page = urllib2.urlopen(population_url)
soup = BeautifulSoup(population_page)
population = soup.find('span',{'id':'usclocknum'}).text
debt_amount = float(national_debt.replace(',',''))
population_amount = int(population.replace(',',''))
per_person = debt_amount / population_amount
print "US National debt as of %s is %s, or %.2f for each person(%s) in the US" % (as_of, debt,per_person,population)
Output:
US National debt as of 01/27/2011 is 14,059,409,159,678.42, or 45064.71 for each person(311,982,692) in the US

Post a Comment