Python 在为Google创建客户端时获取KeyError’;s使用urllib、urllib2和json模块映射API

Python 在为Google创建客户端时获取KeyError’;s使用urllib、urllib2和json模块映射API,python,json,urllib2,Python,Json,Urllib2,执行此代码时,我收到一个错误: 回溯(最近一次呼叫最后一次): 文件“C:/Python27/Foundations\u of_networking/search2.py”,第11行,在 打印(回复['Placemark'][0]['Point']['coordinates'][:-1]) KeyError:“Placemark” 如果有人知道解决办法,请帮助我。我刚刚接触python。只要打印出原始回复,查看它有哪些键,然后访问这些键,如果您这样做: import urllib,urllib2

执行此代码时,我收到一个错误:

回溯(最近一次呼叫最后一次): 文件“C:/Python27/Foundations\u of_networking/search2.py”,第11行,在 打印(回复['Placemark'][0]['Point']['coordinates'][:-1]) KeyError:“Placemark”


如果有人知道解决办法,请帮助我。我刚刚接触python。

只要打印出原始回复,查看它有哪些键,然后访问这些键,如果您这样做:

import urllib,urllib2
try:
    import json
except ImportError:
    import simplejson as json
params = {'q': '207 N. Defiance St, Archbold, OH','output': 'json', 'oe': 'utf8'}
url = 'http://maps.google.com/maps/geo?' + urllib.urlencode(params)

rawreply = urllib2.urlopen(url).read()
reply = json.loads(rawreply)
print (reply['Placemark'][0]['Point']['coordinates'][:-1])
您将获得:

print (reply["Status"]), 
您的整个JSON如下所示:

{u'code': 610, u'request': u'geocode'}
import urllib,urllib2
try:
    import json
except ImportError:
    import simplejson as json
params = {'address': '207 N. Defiance St, Archbold, OH', 'sensor' : 'false', 'oe': 'utf8'}
url = 'http://maps.googleapis.com/maps/api/geocode/json?' + urllib.urlencode(params)

rawreply = urllib2.urlopen(url).read()
reply = json.loads(rawreply)

if reply['status'] == 'OK':
    #supports multiple results
    for item in reply['results']:
        print (item['geometry']['location'])

    #always chooses first result
    print (reply['results'][0]['geometry']['location'])
else:
    print (reply)
因此,如果您想访问代码,只需执行以下操作:

{u'Status': {u'code': 610, u'request': u'geocode'}}

当您试图访问对象上不存在的密钥时,会引发KeyError异常

我在答复中打印答复文本:

print(reply["Status"]["code"])
所以问题是您没有收到预期的响应,其中没有“Placemark”键,因此出现了异常


在Google Maps API上查找代码610的含义,可能您没有进行正确的查询,或者您必须在访问之前检查响应。

如果您只打印
回复
,您将看到以下内容:

>>> print rawreply
... {
      "Status": {
        "code": 610,
        "request": "geocode"
      }
    }
您使用的是不推荐使用的API版本。移动到v3。请看报纸顶部的通知

我以前没有使用过此API,但以下提示(摘自):

新端点

v3地理编码器使用不同的URL端点:

在哪里 输出可以指定为json或xml

从v2切换的开发人员可能使用传统的主机名—或者 如果使用ssl,请访问maps.google.com或maps-api-ssl.google.com。你应该 迁移到新主机名:maps.googleapis.com。此主机名可以是 在HTTPS和HTTP上同时使用

请尝试以下操作:

{u'Status': {u'code': 610, u'request': u'geocode'}}
上面我展示了两种访问结果经度和纬度的方法。
for
循环将支持返回多个结果的情况。第二个简单地选择第一个结果。请注意,在这两种情况下,我首先检查返回的
状态
,以确保返回真实数据

如果要独立访问纬度和经度,可以这样做:

{u'code': 610, u'request': u'geocode'}
import urllib,urllib2
try:
    import json
except ImportError:
    import simplejson as json
params = {'address': '207 N. Defiance St, Archbold, OH', 'sensor' : 'false', 'oe': 'utf8'}
url = 'http://maps.googleapis.com/maps/api/geocode/json?' + urllib.urlencode(params)

rawreply = urllib2.urlopen(url).read()
reply = json.loads(rawreply)

if reply['status'] == 'OK':
    #supports multiple results
    for item in reply['results']:
        print (item['geometry']['location'])

    #always chooses first result
    print (reply['results'][0]['geometry']['location'])
else:
    print (reply)

@Brian..上面的代码正在运行。。但是,如果我只想查看纬度和经度,那么在上面的代码中我必须做哪些更改。@user2433888我已经更新了答案以显示访问coordinates@Pawelmhm... 如果我只想看到纬度和经度,那么我必须在上面的代码中做些什么更改。