skip to Main Content

What I need to do:

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.

Extra Info:

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

What I need to retrieve:

plus_code of Rochester Institute of Technology

import urllib.parse
import urllib.request
import json

def get_plus_code(location):
    try:
        # Construct the URL with the location query
        base_url = 'http://www.py4e.com/code3/opengeo.py'
        url = base_url + urllib.parse.urlencode({'q': location})

        # Contact the web service and retrieve JSON data
        response = urllib.request.urlopen(url)
        data = json.loads(response.read().decode())

        # Parse the JSON data and retrieve the first plus_code
        if 'results' in data and data['results']:
            first_result = data['results'][0]
            if 'plus_code' in first_result:
                plus_code = first_result['plus_code']
                return plus_code
        return None

    except Exception as e:
        print("An error occurred:", e)
        return None

# Location to search for
location = "Rochester Institute of Technology"

# Retrieve plus_code for the location
plus_code = get_plus_code(location)
if plus_code:
    print("Plus code for", location + ":", plus_code)
else:
    print("No plus code found for the location.")

When using this code I get no matter what link I use:

s/Calling a JSON API.py" An error occurred: HTTP Error 403: Forbidden
No plus code found for the location.

2

Answers


  1. As JonSG wrote in a comment five hours ago,

    You want base_url = "http://py4e-data.dr-chuck.net/opengeo?"

    That’s because the comparison you use in https://www.py4e.com/code3/opengeo.py on line five goes

    serviceurl = ‘https://py4e-data.dr-chuck.net/opengeo?’

    whereas your code is currently

    base_url = ‘http://www.py4e.com/code3/opengeo.py’

    , probably just a typo or a lapse of mind then.

    To me looks like that’s the only thing barring success.
    Kudos for asking this question btw, the API you’re learning to use is really inspirational! I have to try a project of my own with it.

    Login or Signup to reply.
  2. This student should really be asking this question on the course forum since it is a graded assignment.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search