【问题标题】:403 Forbidden error when geocoding地理编码时出现 403 禁止错误
【发布时间】:2014-02-08 16:24:14
【问题描述】:

我正在运行以下代码

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

知道如何解决这个错误吗?我怎么也想不通。

【问题讨论】:

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

标签: python google-maps google-maps-api-3 maps


【解决方案1】:

好吧,事实证明 Google 地图使用公共 IP。我参加了一场黑客马拉松,其他一些参与者已经完成了对该公共 IP 上每个人的配额阻止请求。

【讨论】:

    【解决方案2】:

    您可以找到解决方案here(来自 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'])
    

    【讨论】:

      猜你喜欢
      • 2011-03-18
      • 2016-01-12
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 2021-10-03
      • 1970-01-01
      相关资源
      最近更新 更多