def main():
    '''
    Purpose::
        Fetches
        http://en.wikipedia.org/wiki/List_of_United_States_district_and_territorial_courts
        and uses XPATH to extract out Regions
        from United States district and territorial courts
        The data is then written to a flat file one region per
        line.
    Input::
    
    Output::
    
    Assumptions::
    '''
    # Get arguments
    args = parse_arguments()
    if args.url:
        url = args.url

    # Make soup
    try:
        resp = urlopen(url)
    except URLError as e:
        print 'An error occured fetching %s \n %s' % (url, e.reason)   
        return 1
    soup = BeautifulSoup(resp.read())

    # Get table
    try:
        table = soup.find_all('table')[3]
    except __HOLE__ as e:
        print 'No tables found, exiting'
        return 1

    # Get rows
    try:
        rows = table.find_all('tr')
    except AttributeError as e:
        print 'No table rows found, exiting'
        return 1

    # Get data
    table_data = parse_rows(rows)

    f = open('US_district_and_territorial_courts.txt', 'w')
    # Print data
    #f.write("\n".join(str(i).encode('utf8') for i in table_data))
    for i in table_data:
        f.write((i).encode('utf8') +'\n')

    f.close()
       
    #write data to flat file
dataset/ETHPy150Open memex-explorer/memex-gate/utility/scrape_district_and_territorial_courts.py/main