Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use Pandas to download html table of bear sightings in CT and save to bears.csv
import pandas as pd
class BearTable():
"""
BearTable() - get a pandas DataFrame() of bear sightings in CT
--------------------------------------------------------------
This is a rolling list of bear sightings maintained by the CT
Department of Energy and Environmental protection. It is
available online at: http://www.depdata.ct.gov/wildlife/sighting/bearsight.asp
This tool will fail if the structure of that web page changes
from when this tool was designed.
"""
def __init__(self):
self.table = False
self.url = "http://www.depdata.ct.gov/wildlife/sighting/bearsight.asp"
tables = pd.read_html(self.url)
# print "Found " + str(len(tables)) + " tables."
table = tables[11]
if (len(table.columns) != 2):
raise Exception("This table doesn't look right. Maybe the page has been changed")
table.columns = "Town","Reports"
self.table = table
def get(self):
return self.table
(BearTable()).get().to_csv("bears.csv",index=False)
# Do the same thing, but in 4 lines
import pandas as pd
table = pd.read_html("http://www.depdata.ct.gov/wildlife/sighting/bearsight.asp")[11]
table.columns = "Towns","Reports"
table.to_csv("bears.csv", index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment