Skip to content

Instantly share code, notes, and snippets.

@mittenchops
Last active August 29, 2015 13:58
Show Gist options
  • Save mittenchops/10277897 to your computer and use it in GitHub Desktop.
Save mittenchops/10277897 to your computer and use it in GitHub Desktop.
Get state FIPS codes from Gov
import requests
import bs4 as BeautifulSoup
def get_states():
page = requests.get("http://www.epa.gov/enviro/html/codes/state.html").content
soup = BeautifulSoup.BeautifulSoup(page)
def findstates(tr):
tds = tr.find_all('td')
for td in tds:
a = td.find_all("a")
if a:
return(a[0].text)
vals = map(findstates, soup.find_all('tr'))
return(filter(None, vals))
states = get_states()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment