Created
January 31, 2011 02:04
-
-
Save neilkod/803533 to your computer and use it in GitHub Desktop.
retrieves and prints the national debt and us population
This file contains hidden or 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
from BeautifulSoup import BeautifulSoup | |
import urllib2 | |
page = urllib2.urlopen('http://www.treasurydirect.gov/NP/BPDLogin?application=np') | |
soup = BeautifulSoup(page) | |
debt = soup.find('table',{'class':'data1'}).findAll('td')[3].text | |
asof = 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(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" % (asof, debt,per_person,population) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Change those "text" statements to "contents" and it works perfectly.