您好,我收到“ JSONDecodeError:预期值:第1行第1列(字符0)”

时间:2019-02-10 17:07:45

标签: python coursera-api

这是代码。

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)

这是我遇到的错误

here is the second image

1 个答案:

答案 0 :(得分:1)

要求:

  • urllib pip install urllib
  • 请求pip install requests

代码

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)

输出

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