Skip to main content
 首页 » 编程设计

python之使用 Google map 进行地理编码时出现 403 错误

2024年11月01日32wuhuacong

我正在尝试使用以下代码,但它不起作用。

from googlemaps import GoogleMaps 
gmaps = GoogleMaps(api_key='mykey') 
reverse = gmaps.reverse_geocode(38.887563, -77.019929) 
address = reverse['Placemark'][0]['address'] 
print(address) 

当我尝试运行这段代码时,出现以下错误。请帮我解决这个问题。

Traceback (most recent call last): 
  File "C:/Users/Gokul/PycharmProjects/work/zipcode.py", line 3, in <module> 
    reverse = gmaps.reverse_geocode(38.887563, -77.019929) 
  File "C:\Python27\lib\site-packages\googlemaps.py", line 295, in reverse_geocode 
    return self.geocode("%f,%f" % (lat, lng), sensor=sensor, oe=oe, ll=ll, spn=spn, gl=gl) 
  File "C:\Python27\lib\site-packages\googlemaps.py", line 259, in geocode 
    url, response = fetch_json(self._GEOCODE_QUERY_URL, params=params) 
  File "C:\Python27\lib\site-packages\googlemaps.py", line 50, in fetch_json 
    response = urllib2.urlopen(request) 
  File "C:\Python27\lib\urllib2.py", line 127, in urlopen 
    return _opener.open(url, data, timeout) 
  File "C:\Python27\lib\urllib2.py", line 410, in open 
    response = meth(req, response) 
  File "C:\Python27\lib\urllib2.py", line 523, in http_response 
    'http', request, response, code, msg, hdrs) 
  File "C:\Python27\lib\urllib2.py", line 448, in error 
    return self._call_chain(*args) 
  File "C:\Python27\lib\urllib2.py", line 382, in _call_chain 
    result = func(*args) 
  File "C:\Python27\lib\urllib2.py", line 531, in http_error_default 
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
urllib2.HTTPError: HTTP Error 403: Forbidden 

请您参考如下方法:

阅读 2010 年编写的《Python 网络编程基础》一书时,我发现自己正在处理类似的错误。稍微更改代码示例,我收到此错误消息:

The Geocoding API v2 has been turned down on September 9th, 2013. The Geocoding API v3 should be used now. Learn more at https://developers.google.com/maps/documentation/geocoding/

显然,url 中所需的 params 和 url 本身发生了变化。 曾经是:

params = {'q': '27 de Abril 1000, Cordoba, Argentina', 
          'output': 'json', 'oe': 'utf8'} 
url = 'http://maps.google.com/maps/geo?' + urllib.urlencode(params) 

现在是:

params = {'address': '27 de Abril 1000, Cordoba, Argentina',                     
          'sensor': 'false'}                                                    
 
url = 'http://maps.googleapis.com/maps/api/geocode/json?' + urllib.urlencode(params) 

我假设 googlemaps python 包还没有更新。

这对我有用。

下面是一个完整的示例,或多或少必须说明 GoogleMap() 在幕后所做的事情:

import urllib, urllib2                                                           
import json                                                                      
 
params = {'address': '27 de Abril 1000, Cordoba, Argentina',                     
          'sensor': 'false'}                                                    
 
url = 'http://maps.googleapis.com/maps/api/geocode/json?' + urllib.urlencode(params) 
 
rawreply = urllib2.urlopen(url).read()                                           
reply = json.loads(rawreply)                                                     
 
lat = reply['results'][0]['geometry']['location']['lat']                         
lng = reply['results'][0]['geometry']['location']['lng']                         
 
print '[%f; %f]' % (lat, lng)