-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGetlocationid.py
More file actions
52 lines (40 loc) · 1.37 KB
/
Getlocationid.py
File metadata and controls
52 lines (40 loc) · 1.37 KB
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
import urllib.request, urllib.parse, urllib.error
import json
fh = open('urlgeo.txt', 'w')
# Note that Google is increasingly requiring keys
# for this API, recheve the information from the website
serviceurl = 'http://py4e-data.dr-chuck.net/geojson?'
while True:
address = input('Enter location: ')
# address = 'University of Buenos Aires'
if len(address) < 1: break
url = serviceurl + urllib.parse.urlencode(
{'address': address})
print('Retrieving', url)
uh = urllib.request.urlopen(url)
data = uh.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
raw_data = json.dumps(js, indent=4)
fh.write(raw_data+'\n')
fh.close()
# load the file information into data
fh = open('urlgeo-1.txt')
data = fh.read()
fh.close()
# extract the inforamtion from Json
raw_handle = json.loads(data)
# print(len(raw_handle)), two keys are status and results
handle_results = raw_handle['results']
#print(type(handle_results),len(handle_results)),
#this is a list with only one value
handle_address = handle_results[0]
place_id = handle_address['place_id']
print(place_id)