Created
November 28, 2011 22:36
-
-
Save phmongeau/1402406 to your computer and use it in GitHub Desktop.
Geolocalistaion des bibliothèques de Montréal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import json | |
import requests | |
import urllib | |
from urllib import urlopen | |
import codecs | |
import time | |
url = u"http://maps.googleapis.com/maps/api/geocode/json?address={0}&sensor=false" | |
def format_spaces(input): | |
return input.replace(" ", "+") | |
def get_coords(place): | |
place = format_spaces(place) | |
request = url.format(place) | |
response = urlopen(request.encode("utf8")) | |
j = json.loads(response.read()) | |
try: | |
coords = j["results"][0]["geometry"]["location"] | |
except: | |
print j | |
return coords["lat"], coords["lng"] | |
if __name__ == '__main__': | |
with open("adresses_bibliotheques.txt") as f: | |
file = codecs.getreader("utf-8")(f).read() | |
lines = file.split('\n') | |
lines.pop() | |
locations = [] | |
for i,l in enumerate(lines): | |
name, adresse = l.split(': ') | |
adresse = adresse.replace('<br>', ' ') | |
lat, lng = get_coords(adresse) | |
out = { | |
"name": name, | |
"latitude": lat, | |
"longitude": lng, | |
"adresse": adresse | |
} | |
locations.append(out) | |
if i%5: | |
time.sleep(0.5) | |
#print json.dumps(out, indent=4, sort_keys=True, ensure_ascii=False), "," | |
#for key, val in out.items(): | |
#print key, val | |
print json.dumps(locations, indent=4, sort_keys=True, ensure_ascii=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from urllib import urlopen | |
import json | |
url = "http://ville.montreal.qc.ca/pls/portal/ubst.lieu_wqp.get_coord_horaire?p_langue={0}&p_id={1}" | |
#individual page: | |
def scrape_page(id, lang="fr"): | |
response = urlopen(url.format(lang, id)).read() | |
if response == "{\n": | |
return "","","" | |
response = response.decode("iso-8859-1") | |
j = json.loads(response) | |
horaire = [] | |
#for i in range(1,8): | |
#horaire.append(j.get("HREGAJ" + i)) | |
return j.get("adresse"), j.get("legende"), horaire | |
if __name__ == '__main__': | |
for i in range(60): | |
adresse, city, hr = scrape_page(i) | |
if adresse and city: | |
result = u"{}: {}".format(city, adresse) | |
print result.encode("utf-8") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment