Skip to content

Instantly share code, notes, and snippets.

@davefernig
Created June 12, 2018 02:42
Show Gist options
  • Select an option

  • Save davefernig/006a5b9761cb858a38830de1ba18b1a0 to your computer and use it in GitHub Desktop.

Select an option

Save davefernig/006a5b9761cb858a38830de1ba18b1a0 to your computer and use it in GitHub Desktop.

Revisions

  1. davefernig created this gist Jun 12, 2018.
    21 changes: 21 additions & 0 deletions statcan
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # Before executing this script, run the following in your terminal:
    # virtualenv .
    # source bin/activate
    # pip install beautifulsoup4

    from bs4 import BeautifulSoup
    import urllib2


    path = "http://www12.statcan.gc.ca/census-recensement/2016/as-sa/98-200-x/2016001/98-200-x2016001-eng.cfm"

    response = urllib2.urlopen(path)
    soup = BeautifulSoup(response.read(), 'html.parser')
    tables = soup.findAll("table")

    for row in tables[0].findAll("tr")[2:-1]:
    data = row.getText().split("\n")
    print data[2].ljust(12),\
    data[3].ljust(18),\
    data[4].ljust(30),\
    data[5].ljust(15)