Skip to content

Instantly share code, notes, and snippets.

@neilkod
Created January 31, 2011 02:04
Show Gist options
  • Save neilkod/803533 to your computer and use it in GitHub Desktop.
Save neilkod/803533 to your computer and use it in GitHub Desktop.
retrieves and prints the national debt and us population
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)
@docblades
Copy link

Change those "text" statements to "contents" and it works perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment