File tree 1 file changed +36
-0
lines changed
Using_Python_to_Access_Web_Data
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ # prompt for a location, contact a web service and retrieve JSON for the web
2
+ # service and parse that data, and retrieve the first place_id from the JSON.
3
+ # A place ID is a textual identifier that uniquely identifies a place as within
4
+ # Google Map
5
+ import urllib .request ,urllib .parse ,urllib .error
6
+ import ssl
7
+ import json
8
+ API_URL = 'http://py4e-data.dr-chuck.net/json?'
9
+
10
+ # Ignore SSL certificate errors
11
+ ctx = ssl .create_default_context ()
12
+ ctx .check_hostname = False
13
+ ctx .verify_mode = ssl .CERT_NONE
14
+ while True :
15
+ location = input ('Enter a Location: ' )
16
+ if len (location )< 1 :
17
+ location = 'South Federal University'
18
+ print ('retrieving default Location: South Federal University' )
19
+ url = API_URL + urllib .parse .urlencode ({'address' :location ,'key' :42 })
20
+ reqHandApi = urllib .request .urlopen (url )
21
+ data = reqHandApi .read ().decode ()
22
+ # print(data)
23
+
24
+ try :
25
+ js = json .loads (data )
26
+ except :
27
+ js = None
28
+
29
+ if not js or 'status' not in js or js ['status' ] != 'OK' :
30
+ print ("ERROR, can't retrieve location" )
31
+ # print(data)
32
+ continue
33
+ place_id = js ['results' ][0 ]['place_id' ]
34
+ print (place_id )
35
+
36
+ break
You can’t perform that action at this time.
0 commit comments