Last active
September 9, 2016 22:24
-
-
Save jakekara/970e6547a9d2a657885862732cda3937 to your computer and use it in GitHub Desktop.
Use Pandas to download html table of bear sightings in CT and save to bears.csv
This file contains 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
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) |
This file contains 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
# 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