-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extracting_jsondata_from_geogle_api.py
56 lines (46 loc) · 1.62 KB
/
Extracting_jsondata_from_geogle_api.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import urllib.request, urllib.parse, urllib.error
import json
import ssl
api_key = False
# If you have a Google Places API key, enter it here
# api_key = 'AIzaSy___IDByT70'
# https://developers.google.com/maps/documentation/geocoding/intro
if api_key is False:
api_key = 42
serviceurl = 'http://py4e-data.dr-chuck.net/json?'
else :
serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?'
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
while True:
address = input('Enter location: ')
if len(address) < 1: break
parameters = dict()
parameters['address'] = address
if api_key is not False:
parameters['key'] = api_key
url = serviceurl + urllib.parse.urlencode(parameters)
print('Retrieved', url)
link = urllib.request.urlopen(url, context = ctx)
data = link.read().decode()
print('Retrieved', len(data),'characters')
try:
js = json.loads(data)
except:
js = None
if not js or 'status' not in js or js['status'] != 'OK':
PRINT('=== Failure To Retrieve ===')
print(data)
continue
print(json.dumps(js, indent=4))
latitude = js['results'][0]['geometry']['location']['lat']
longitude = js['results'][0]['geometry']['location']['lng']
place_id = js['results'][0]['place_id']
location = js['results'][0]['formatted_address']
print()
print('Place id:', place_id)
print('Latitude:', latitude,'longitude:',longitude)
print('Location:',location)
print()