I am learning Python from coursera and following this assigment but i cant found a solve of this problem, i tryed this code, but i cant found the plus_code. thank you for your contributions. would be of great help to me.
THE ASSIGNMENT
Calling a JSON API
In this assignment you will write a Python program somewhat similar to http://www.py4e.com/code3/opengeo.py. The program will prompt for a location, contact a web service and retrieve JSON for the web service and parse that data, and retrieve the first plus_code from the JSON. An Open Location Code is a textual identifier that is another form of address based on the location of the address.
API End Points
To complete this assignment, you should use this API endpoint that has a static subset of the Open Street Map Data.
http://py4e-data.dr-chuck.net/opengeo?
This API also has no rate limit so you can test as often as you like. If you visit the URL with no parameters, you get “No address…” response.
To call the API, you need to provide the address that you are requesting as the q= parameter that is properly URL encoded using the urllib.parse.urlencode() function as shown in http://www.py4e.com/code3/opengeo.py
Test Data / Sample Execution
You can test to see if your program is working with a location of “South Federal University” which will have a plus_code of “6FV8QPRJ+VQ”.
$ python solution.py
Enter location: South Federal University
Retrieving http://…
Retrieved 1290 characters
Plus code 6FV8QPRJ+VQ
Turn In
Please run your program to find the plus_code for this location:
University of Chicago
Make sure to enter the name and case exactly as above and enter the plus_code and your Python code below. Hint: The first five characters of the plus_code are “86HJQ …”
Make sure to retreive the data from the URL specified above and not the normal Google API. Your program should work with the Google API – but the plus_code may not match for this assignment.
plus_code:
Python code:
**MY CODE: **
import urllib.request
import urllib.parse
import json
serviceurl = ‘http://py4e-data.dr-chuck.net/opengeo?’
while True:
address = input(‘Enter location: ‘)
if len(address) < 1: break
url = serviceurl + urllib.parse.urlencode({'q': 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 'results' not in js or len(js['results']) < 1:
print('==== Failure To Retrieve ====')
continue
plus_code = js['results'][0]['plus_code']
print('Plus code', plus_code)
I am learning Python from coursera and following this assigment but i cant found a solve of this problem, i tryed this code, but i cant found the plus_code. thank you for your contributions. would be of great help to me.
Daniel Hernandez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.