Python Forum

Full Version: Using the GeoJSON API
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The location I've been given to input is Indian Institute of Technology Kharagpur India, but somehow I'm getting an error with my code and hence not been able to get a final output.
Below is the code I had written, kindly let me know if I'm doing something wrong.
Regard.
----------------------------------
import urllib
import json

serviceurl = "http://py4e-data.dr-chuck.net/json?"

while True:

    address = input("Enter location: ")

    if len(address) < 1 : break

    url = serviceurl + urllib.urlencode({'sensor':'false','address':address})

    print('Retrieving',url)

    uh =urllib.urlopen(url)
    data = uh.read()
    print('Retrived',len(data),'characters')

    try: js = json.loads(str(data))
    except: js = None
        print('==== Failure To Retrieve ====')
        print(data)
        continue

    placeid = js["results"][0]['place_id']
    print("Place id",placeid)
#!/usr/bin/python3
import requests
from pprint import pprint

url = "http://py4e-data.dr-chuck.net/comments_42.json"
rsp = requests.get(url)
if rsp.status_code == 200:
    json_data = rsp.json()
    pprint(json_data)
else:
    print(f"ERROR: {rsp.status_code}")
    print(rsp.text)