In [2]:
import pandas as pd

In [3]:
CRA_LIST_OF_MUNICIPALITIES = {
    'Alberta': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-alberta.html',
    'British Columbia': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-british-columbia.html',
    'Manitoba': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-manitoba.html',
    'New Brunswick': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-new-brunswick.html',
    'Newfoundland and Labrador': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-newfoundland-labrador.html',
    'Northwest Territories': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-northwest-territories.html',
    'Nova Scotia': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-nova-scotia.html',
    'Nunavut': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-nunavut.html',
    'Ontario': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-ontario.html',
    'Prince Edward Island': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-prince-edward-island.html',
    'Quebec': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-quebec.html',
    'Saskatchewan': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-saskatchewan.html',
    'Yukon': 'https://www.canada.ca/en/revenue-agency/services/charities-giving/other-organizations-that-issue-donation-receipts-qualified-donees/other-qualified-donees-listings/list-municipalities-yukon.html',
}

In [4]:
dfs = []

for province, website in CRA_LIST_OF_MUNICIPALITIES.items():
    df = pd.read_html(website)[0]
    
    df['Province'] = province
    
    dfs.append(df)

df = pd.concat(dfs)

df.tail()


Out[4]:
Name Type Status Effective date Notes Province
3 Haines Junction Municipality Registered 2012-01-01 NaN Yukon
4 Mayo Municipality Registered 2012-01-01 NaN Yukon
5 Teslin Municipality Registered 2012-01-01 NaN Yukon
6 Watson Lake Municipality Registered 2012-01-01 NaN Yukon
7 Whitehorse Municipality Registered 2012-01-01 NaN Yukon

In [5]:
df = df[['Name', 'Province', 'Type', 'Status', 'Effective date', 'Notes']]

df['Effective date'] = pd.to_datetime(df['Effective date'])

df = df.rename(columns={'Name': 'name', 'Province': 'province', 'Type': 'type',
                   'Status': 'status', 'Effective date': 'effective_date', 'Notes': 'notes'})

df.tail()


Out[5]:
name province type status effective_date notes
3 Haines Junction Yukon Municipality Registered 2012-01-01 NaN
4 Mayo Yukon Municipality Registered 2012-01-01 NaN
5 Teslin Yukon Municipality Registered 2012-01-01 NaN
6 Watson Lake Yukon Municipality Registered 2012-01-01 NaN
7 Whitehorse Yukon Municipality Registered 2012-01-01 NaN

In [6]:
import datetime

df.to_csv('list-of-canadian-municipalities.csv'.format(datetime.date.today()))

In [8]:
with open('list-of-canadian-municipalities.json', 'w') as fobj:
    fobj.write(df.to_json(orient='table'))

In [ ]: