Login  Register

MPs by postal code

Posted by Daniel Haran on Mar 21, 2007; 7:25pm
URL: http://civicaccess.416.s1.nabble.com/MPs-by-postal-code-tp959.html

All right, since a few people said they would actually use this, I
decided to try extracting the list of MPs by postal code.

Tools like the lobby module in Drupal try to extract the information
from the page - an arduous task since the web page is a *mess*.
Already having the list of MPs, I only needed to extract the email
address.

If I have time this week-end, it will go inside the mp-scraper, with a
REST interface for anyone to use. Here's the code for you geeks:

require 'rubygems'
require 'hpricot'
require 'open-uri'

#postal_code = 'A1A 1A1'.gsub(/ /, '')
postal_code = 'H1T4C6'.gsub(/ /, '')

doc = Hpricot(open('http://www.parl.gc.ca/information/about/people/house/PostalCode.asp?Language=E&txtPostalCode='+postal_code))

emails = (doc/"h4").select {|e| e.innerHTML =~ /Parliament/}.collect
{|e| e.next_sibling.innerHTML.match(/ (.*@parl\.gc\.ca)/)[1]}

mps = Mp.find_all_by_email(emails)

Sample output on H1T4C6:
>>emails
=> ["[hidden email]", "[hidden email]",
"[hidden email]", "[hidden email]",
"[hidden email]"]

Cheerio,

Daniel.

--
Change the world one loan at a time - visit Kiva.org to find out how