ImportUrllibImportUrllib.requestImportJsonserviceurl =' Http://maps.googleapis.com/maps/api/geocode/json? '#serviceurl = ' Http://python-data.dr-chuck.net/geojson? 'While True: Address =Input' Enter location: ')ifLen (address) <1: Breakurl = serviceurl + urllib.parse.urlencode ({' Sensor ':' False ',' Address ': address})Print' Retrieving: ', url) uh = urllib.request.urlopen (URL) data = Uh.read ()Print' Retrieved: ',Len (data),' characters ')PrintSTR (data))#接受过来的数据都是字节型数据需要进行decode () operation Try: JS = json.loads (Data.decode ())except: JS =None#try: JS = json.loads (str (data))#except: js = None Print (JS)if' Status ' not inJsorjs[' status ']! =' OK ':Print' ===failed to retrieve=== ')Print (data)ContinuePrint (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)
Python calls the Google Map API