-1

I have a database with addresses and I would like to run a script to scrape the lat lon info from Google Maps and input it into the database.

I would like to call a site by inputing the address like this: http://maps.google.com/maps?q=1600+Amphitheatre+Parkway+Mountain+View+CA+94043

where eveything before the q= is static and everything after is generated by script.

When you click on the address, this is returned: https://www.google.com/maps/place/1600+Amphitheatre+Pkwy,+Mountain+View,+CA+94043/@37.4224879,-122.08422,17z/data=!3m1!4b1!4m2!3m1!1s0x808fba027820e5d9:0x60a90600ff6e7e6e

How do I A)program Python to visit the first website, then save the returned site to a variable, and B) pull the two numbers after the @ into 2 separate variables?

pekasus
  • 626
  • 2
  • 7
  • 20

1 Answers1

0

You can use Google API v3 too. Based on the answer GoogleMaps API -address to coordinates (latitude,longitude)

import urllib
import simplejson
googleGeocodeUrl = 'http://maps.googleapis.com/maps/api/geocode/json?'
def get_coordinates(query, from_sensor):
    query = query.encode('utf-8')
    params = {
        'address': query,
        'sensor': "true" if from_sensor else "false"
    }
    url = googleGeocodeUrl + urllib.urlencode(params)
    json_response = urllib.urlopen(url)
    response = simplejson.loads(json_response.read())
    if response['results']:
        location = response['results'][0]['geometry']['location']
        latitude, longitude = location['lat'], location['lng']
        print query, latitude, longitude
    else:
        latitude, longitude = None, None
        print query, "<no results>"
        print
    return latitude, longitude


u  = 'http://maps.google.com/maps?q=1601+Amphitheatre+Parkway+Mountain+View+CA+94043'
get_coordinates(u[30:],'False')

1601+Amphitheatre+Parkway+Mountain+View+CA+94043 37.4240679 -122.0859093

Community
  • 1
  • 1
jaumetet
  • 86
  • 7