-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson32.py
31 lines (23 loc) · 881 Bytes
/
lesson32.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Lesson 32 an API example
import urllib.request, urllib.parse, urllib.error
import json
service = 'http://maps.googleapis.com/maps/api/geocode/json?'
while True :
location = input('Enter a location: ')
if len(location) < 1 : break
url = service + urllib.parse.urlencode({'address': location})
handle = urllib.request.urlopen(url)
data = handle.read().decode()
print('data', data)
try :
js = json.loads(data)
except :
js = None
if not js or 'status' not in js or js['status'] != 'OK':
print('Failure to retrieve')
continue
lat = js['results'][0]['geometry']['location']['lat']
lng = js['results'][0]['geometry']['location']['lng']
loc = js['results'][0]['formatted_address']
print('Latitude:',lat,'Longitude:',lng,'Location:',loc)
# RETURNS >>>> You must use an API key to authenticate