"""Rough brute-force script to extract coordinates for specific search results from Google Maps. Should really have used a proper XML parser, but the XML is embedded in a string inside JavaScript code in the returned HTML page, which makes it kind of awkward. Anyway, this works for now. $Id: gxy.py,v 1.4 2009/08/04 20:15:20 larsga Exp $ """ import sys, urllib, string, re def extract_point(reg, data, pos): m = reg.search(data, pos) assert m, "Latitude/longitude not found for point" return m.group(1) def get_multihit_info(data, hitno): reg_point = re.compile(" 0: m = reg_point.search(data, start) if not m: break hitno = hitno - 1 start = m.end() if not m: print "No match found" sys.exit(1) pos = m.end() reg_lat = re.compile('lat="([^"]+)"') reg_long = re.compile('lng="([^"]+)"') lat = extract_point(reg_lat, data, pos) long = extract_point(reg_long, data, pos) print "Longitude: %s, latitude: %s" % (long, lat) def try_address_marker(data): if string.find(data, 'id:"addr"') == -1: return None # {id:"addr",lat:59.323132999999999,lng:18.071866, reg_marker = re.compile("\\{id:\s*(\"|')addr(\"|'),\s*lat:\s*([^,]+),\s*lng:\s*([^,]+)") m = reg_marker.search(data) assert m return (m.group(4), m.group(3)) def try_approx_hit(data): if string.find(data, ",approx:true,") == -1: return None # markers:[{id:"cent",lat:59.317428,lng:18.050695999999999, reg_marker = re.compile("markers:\\[\\{id:(\"|')cent(\"|'),lat:([^,]+),lng:([^,]+),") m = reg_marker.search(data) assert m return (float(m.group(4)), float(m.group(3))) def try_new_format(data): if string.find(data, ',geocode:"') == -1: return None reg_marker = re.compile("latlng:{lat:(-?\d+\\.\d+),lng:(-?\d+\\.\d+)") m = reg_marker.search(data) assert m return (m.group(2), m.group(1)) def prettify(num): if len(num) > 10: last = num[-1] num = num[ : -1] # remove last random digit while num[-1] in "09": last = num[-1] num = num[ : -1] if last == "9": num = num[ : -1] + str(int(num[-1]) + 1) return num # --- query = urllib.quote(sys.argv[1]) if len(sys.argv) > 2: hitno = ord(string.upper(sys.argv[2])) - 64 else: hitno = -1 file = urllib.urlopen("http://maps.google.com/local?q=%s&output=js" % query) data = file.read() print data point = try_approx_hit(data) if not point: point = try_address_marker(data) if not point: point = try_new_format(data) point = (prettify(point[0]), prettify(point[1])) print "Longitude: %s, latitude: %s" % point # if hitno != -1: # get_multihit_info(data, hitno) # else: # get_address_marker(data)