0

Here is the code..

address_input = input("Enter location: ")
params = {"sensor": "false", "address": address_input}
serviceurl = "http://py4e-data.dr-chuck.net/json?"
url = serviceurl + urllib.parse.urlencode(params)
print("Retrieving ", url)
data = urllib.request.urlopen(url).read().decode('utf-8')
print('Retrieved', len(data), 'characters')
json_obj = json.loads(data)
place_id = json_obj["results"][0]["place_id"]
print("Place id", place_id)

and here is the error that i am getting

here is the second image

  • I'm _guessing_ that you have JSONlines as a response, not JSON – roganjosh Feb 10 '19 at 17:09
  • I tried to edit your title and that exact error already exiats as a question. Please research your issue begore asking. – roganjosh Feb 10 '19 at 17:10
  • Have you even thought of printing `data` because that is presumably not json. Pretty basic debugging 101. And please edit yur code to be MCVE https://www.stackoverflow.com/help/mcve to help potential answerers understand precisely the setup/problem – DisappointedByUnaccountableMod Feb 10 '19 at 18:32

1 Answers1

0

Requires:

  • urllib pip install urllib
  • requests pip install requests

Code

import json
import urllib
import requests

address_input = input("Enter location: ")
params = {"sensor": "false", "address": address_input, "key": 42}

serviceurl = "http://py4e-data.dr-chuck.net/json?"
url = serviceurl + urllib.parse.urlencode(params)

print("Retrieving ", url)
resp = requests.get(url)
print('Retrieved', len(resp.content), 'characters')

json_obj = json.loads(resp.content)
place_id = json_obj["results"][0]["place_id"]

print("Place id", place_id)

Output

Enter location: UK
Retrieving  http://py4e-data.dr-chuck.net/json?sensor=false&address=UK&key=42
Retrieved 1196 characters
Place id ChIJqZHHQhE7WgIReiWIMkOg-MQ
Joe Tilsed
  • 324
  • 2
  • 13