Python 403地理编码时禁止的错误

Python 403地理编码时禁止的错误,python,google-maps,google-maps-api-3,maps,Python,Google Maps,Google Maps Api 3,Maps,我正在运行以下代码 from googlemaps import GoogleMaps gmaps = GoogleMaps('my api key here') address = 'Constitution Ave NW & 10th St NW, Washington, DC' lat, lng = gmaps.address_to_latlng(address) print lat, lng 但是当我运行这个时,我得到了以下错误 File "map.py", line 4,

我正在运行以下代码

from googlemaps import GoogleMaps
gmaps = GoogleMaps('my api key here')
address = 'Constitution Ave NW & 10th St NW, Washington, DC'
lat, lng = gmaps.address_to_latlng(address)
print lat, lng
但是当我运行这个时,我得到了以下错误

  File "map.py", line 4, in <module>
lat, lng = gmaps.address_to_latlng(address)
  File "/usr/local/lib/python2.7/dist-packages/googlemaps-1.0.2-py2.7.egg/googlemaps.py", line 310, in address_to_latlng
return tuple(self.geocode(address)['Placemark'][0]['Point']['coordinates'][1::-1])
  File "/usr/local/lib/python2.7/dist-packages/googlemaps-1.0.2-py2.7.egg/googlemaps.py", line 259, in geocode
url, response = fetch_json(self._GEOCODE_QUERY_URL, params=params)
  File "/usr/local/lib/python2.7/dist-packages/googlemaps-1.0.2-py2.7.egg/googlemaps.py", line 50, in fetch_json
response = urllib2.urlopen(request)
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
 File "/usr/lib/python2.7/urllib2.py", line 406, in open
response = meth(req, response)
 File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
'http', request, response, code, msg, hdrs)
 File "/usr/lib/python2.7/urllib2.py", line 444, in error
return self._call_chain(*args)
 File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
 File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden
文件“map.py”,第4行,在
lat,lng=GMAP.地址至latlng(地址)
文件“/usr/local/lib/python2.7/dist packages/googlemaps-1.0.2-py2.7.egg/googlemaps.py”,第310行,地址为
返回元组(self.geocode(address)['Placemark'][0]['Point']['coordinates'][1::-1])
文件“/usr/local/lib/python2.7/dist packages/googlemaps-1.0.2-py2.7.egg/googlemaps.py”,第259行,地理代码
url,response=fetch\u json(self.\u GEOCODE\u QUERY\u url,params=params)
文件“/usr/local/lib/python2.7/dist packages/googlemaps-1.0.2-py2.7.egg/googlemaps.py”,第50行,fetch_json
response=urllib2.urlopen(请求)
文件“/usr/lib/python2.7/urllib2.py”,第126行,在urlopen中
return\u opener.open(url、数据、超时)
文件“/usr/lib/python2.7/urllib2.py”,第406行,打开
响应=方法(请求,响应)
http_响应中的文件“/usr/lib/python2.7/urllib2.py”,第519行
“http”、请求、响应、代码、消息、hdrs)
文件“/usr/lib/python2.7/urllib2.py”,第444行出错
返回自我。调用链(*args)
文件“/usr/lib/python2.7/urllib2.py”,第378行,在调用链中
结果=func(*args)
文件“/usr/lib/python2.7/urllib2.py”,第527行,默认为http\u error\u
raise HTTPError(请求获取完整url(),代码,消息,hdrs,fp)
urllib2.HTTPError:HTTP错误403:禁止

你知道如何修复这个错误吗?我想不出有什么办法。

好吧,原来谷歌地图使用的是公共IP。我参加了一次黑客竞赛,其他一些参与者已经完成了该公共IP上每个人的配额阻止请求。

您可以找到解决方案(来自furrypet的第二个答案)。我简化了一点:

import urllib, json

def geocode(addr):
    url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false" %   (urllib.quote(addr.replace(' ', '+')))
    data = urllib.urlopen(url).read()
    info = json.loads(data).get("results")[0].get("geometry").get("location")

    return info

address = 'Constitution Ave NW & 10th St NW, Washington, DC'

r = geocode(address)
print "%s %s" % (r['lat'], r['lng'])

您是作为一个纯客户端运行还是通过某个wamp服务器运行?没有wamp服务器…只是通过命令行运行它