在 Python 中解析来自 MapQuest 反向地理编码 api 的数据?
Parsing data from MapQuest reverse geocoding api in Python?
我的代码:
from urllib import request
import json
lat = 31.33 ; long = -84.52
webpage = "http://www.mapquestapi.com/geocoding/v1/reverse?key=MY_KEY&callback=renderReverse&location={},{}".format(lat, long)
response = request.urlopen(webpage)
json_data = response.read().decode(response.info().get_param('charset') or 'utf-8')
data = json.loads(json_data)
print(data)
这给了我以下错误:
ValueError: Expecting value: line 1 column 1 (char 0)
我正在尝试从 MapQuest 反向地理编码中读取县和州 api。响应如下所示:
renderReverse({"info":{"statuscode":0,"copyright":{"text":"\u00A9 2015 MapQuest, Inc.","imageUrl":"http://api.mqcdn.com/res/mqlogo.gif","imageAltText":"\u00A9 2015 MapQuest, Inc."},"messages":[]},"options":{"maxResults":1,"thumbMaps":true,"ignoreLatLngInput":false},"results":[{"providedLocation":{"latLng":{"lat":32.841516,"lng":-83.660992}},"locations":[{"street":"562 Patterson St","adminArea6":"","adminArea6Type":"Neighborhood","adminArea5":"Macon","adminArea5Type":"City","adminArea4":"Bibb","adminArea4Type":"County","adminArea3":"GA","adminArea3Type":"State","adminArea1":"US","adminArea1Type":"Country","postalCode":"31204-3508","geocodeQualityCode":"P1AAA","geocodeQuality":"POINT","dragPoint":false,"sideOfStreet":"L","linkId":"0","unknownInput":"","type":"s","latLng":{"lat":32.84117,"lng":-83.660973},"displayLatLng":{"lat":32.84117,"lng":-83.660973},"mapUrl":"http://www.mapquestapi.com/staticmap/v4/getmap?key=MY_KEY&type=map&size=225,160&pois=purple-1,32.84117,-83.660973,0,0,|¢er=32.84117,-83.660973&zoom=15&rand=-189494136"}]}]})
如何将此字符串转换为可以使用键从中查询的字典?任何帮助,将不胜感激。谢谢!
首先,删除 URL 中的回调参数,因为这是导致响应被包装在 renderReverse()
中的原因
webpage = "http://www.mapquestapi.com/geocoding/v1/reverse?key=MY_KEY&location={},{}".format(lat, long)
这将为您提供有效的 json,它应该与您调用的 json.loads 函数一起使用。此时,您可以与字典等数据进行交互,通过键获取县名和州名。 mapquests 构造它们的 json 的方式很奇怪,因此看起来您可能必须进行一些字符串匹配才能获得正确的键名。在这种情况下,'adminArea4Type' 设置为 'County',因此您想要访问 'adminArea4' 键以 return 县名。
data['results'][0]['locations'][0]['adminArea4']
我的代码:
from urllib import request
import json
lat = 31.33 ; long = -84.52
webpage = "http://www.mapquestapi.com/geocoding/v1/reverse?key=MY_KEY&callback=renderReverse&location={},{}".format(lat, long)
response = request.urlopen(webpage)
json_data = response.read().decode(response.info().get_param('charset') or 'utf-8')
data = json.loads(json_data)
print(data)
这给了我以下错误:
ValueError: Expecting value: line 1 column 1 (char 0)
我正在尝试从 MapQuest 反向地理编码中读取县和州 api。响应如下所示:
renderReverse({"info":{"statuscode":0,"copyright":{"text":"\u00A9 2015 MapQuest, Inc.","imageUrl":"http://api.mqcdn.com/res/mqlogo.gif","imageAltText":"\u00A9 2015 MapQuest, Inc."},"messages":[]},"options":{"maxResults":1,"thumbMaps":true,"ignoreLatLngInput":false},"results":[{"providedLocation":{"latLng":{"lat":32.841516,"lng":-83.660992}},"locations":[{"street":"562 Patterson St","adminArea6":"","adminArea6Type":"Neighborhood","adminArea5":"Macon","adminArea5Type":"City","adminArea4":"Bibb","adminArea4Type":"County","adminArea3":"GA","adminArea3Type":"State","adminArea1":"US","adminArea1Type":"Country","postalCode":"31204-3508","geocodeQualityCode":"P1AAA","geocodeQuality":"POINT","dragPoint":false,"sideOfStreet":"L","linkId":"0","unknownInput":"","type":"s","latLng":{"lat":32.84117,"lng":-83.660973},"displayLatLng":{"lat":32.84117,"lng":-83.660973},"mapUrl":"http://www.mapquestapi.com/staticmap/v4/getmap?key=MY_KEY&type=map&size=225,160&pois=purple-1,32.84117,-83.660973,0,0,|¢er=32.84117,-83.660973&zoom=15&rand=-189494136"}]}]})
如何将此字符串转换为可以使用键从中查询的字典?任何帮助,将不胜感激。谢谢!
首先,删除 URL 中的回调参数,因为这是导致响应被包装在 renderReverse()
中的原因webpage = "http://www.mapquestapi.com/geocoding/v1/reverse?key=MY_KEY&location={},{}".format(lat, long)
这将为您提供有效的 json,它应该与您调用的 json.loads 函数一起使用。此时,您可以与字典等数据进行交互,通过键获取县名和州名。 mapquests 构造它们的 json 的方式很奇怪,因此看起来您可能必须进行一些字符串匹配才能获得正确的键名。在这种情况下,'adminArea4Type' 设置为 'County',因此您想要访问 'adminArea4' 键以 return 县名。
data['results'][0]['locations'][0]['adminArea4']