nick34992
6/1/2016 - 5:24 PM

Use Python to Parse JSON API for Place ID

Use Python to Parse JSON API for Place ID

#!/usr/bin/python2.7

import urllib
import json

# serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
serviceurl = 'http://python-data.dr-chuck.net/geojson?'
# Enter: University of Kansas
while True:
    address = raw_input('Enter location: ')
    if len(address) < 1 : break

    url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
    print 'Retrieving', url
    uh = urllib.urlopen(url)
    data = uh.read()
    print 'Retrieved',len(data),'characters'

    try: js = json.loads(str(data))
    except: js = None
    if 'status' not in js or js['status'] != 'OK':
        print '==== Failure To Retrieve ===='
        print data
        continue

    print json.dumps(js, indent=4)

    # lat = js["results"][0]["geometry"]["location"]["lat"]
    # lng = js["results"][0]["geometry"]["location"]["lng"]
    # print 'lat',lat,'lng',lng
    # location = js['results'][0]['formatted_address']
    # print location

    uok = js["results"][0]["place_id"]
    print 'Place ID:', uok