What is the right way to handle errors?
<p>My script below scrapes a website and returns the data from a table. It's not finished but it works.  The problem is that it has no error checking.  Where should I have error handling in my script?</p>

<p>There are no unittests, should I write some and schedule my unittests to be run periodicaly. Or should the error handling be done in my script?</p>

<p>Any advice on the proper way to do this would be great.</p>

<pre><code>#!/usr/bin/env python
''' Gets the Canadian Monthly Residential Bill Calculations table
    from URL and saves the results to a sqllite database.
'''
import urllib2
from BeautifulSoup import BeautifulSoup


class Bills():
    ''' Canadian Monthly Residential Bill Calculations '''

    URL = "http://www.hydro.mb.ca/regulatory_affairs/energy_rates/electricity/utility_rate_comp.shtml"

    def __init__(self):
        ''' Initialization '''

        self.url = self.URL
        self.data = []
        self.get_monthly_residential_bills(self.url)

    def get_monthly_residential_bills(self, url):
        ''' Gets the Monthly Residential Bill Calculations table from URL '''

        doc = urllib2.urlopen(url)
        soup = BeautifulSoup(doc)
        res_table = soup.table.th.findParents()[1]
        results = res_table.findNextSibling()
        header = self.get_column_names(res_table)
        self.get_data(results)
        self.save(header, self.data)

    def get_data(self, results):
        ''' Extracts data from search result. '''

        rows = results.childGenerator()
        data = []
        for row in rows:
            if row == "\n":
                continue
            for td in row.contents:
                if td == "\n":
                    continue
                data.append(td.text)
            self.data.append(tuple(data))
            data = []

    def get_column_names(self, table):
        ''' Gets table title, subtitle and column names '''

        results = table.findAll('tr')
        title = results[0].text
        subtitle = results[1].text
        cols = results[2].childGenerator()
        column_names = []
        for col in cols:
            if col == "\n":
                continue
            column_names.append(col.text)

        return title, subtitle, column_names

    def save(self, header, data):
        pass

if __name__ == '__main__':
    a = Bills()
    for td in a.data:
        print td
</code></pre>
