2

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

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
4

2 回答 2

2

阅读 2010 年编写的《Python 网络编程基础》一书,我发现自己遇到了类似的错误。稍微更改代码示例,我得到了这个错误消息:

Geocoding API v2 已于 2013 年 9 月 9 日关闭。现在应该使用 Geocoding API v3。在https://developers.google.com/maps/documentation/geocoding/了解更多信息

显然,paramsurl中的需要和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)
于 2014-01-02T13:25:03.277 回答
0

在此处阅读有关您遇到的错误的一些内容,表明这可能是由于请求未传递足够的标头以使响应正确返回而引起的。

https://stackoverflow.com/a/13303773/220710

查看包的源代码GoogleMaps,您可以看到它fetch_json在没有 headers 参数的情况下被调用:

...
    url, response = fetch_json(self._GEOCODE_QUERY_URL, params=params)
    status_code = response['Status']['code']
    if status_code != STATUS_OK:
        raise GoogleMapsError(status_code, url, response)
    return response

这是fetch_json函数,所以headers参数似乎是空的{},所以也许这就是问题所在:

def fetch_json(query_url, params={}, headers={}):       # pylint: disable-msg=W0102
    """Retrieve a JSON object from a (parameterized) URL.

    :param query_url: The base URL to query
    :type query_url: string
    :param params: Dictionary mapping (string) query parameters to values
    :type params: dict
    :param headers: Dictionary giving (string) HTTP headers and values
    :type headers: dict 
    :return: A `(url, json_obj)` tuple, where `url` is the final,
    parameterized, encoded URL fetched, and `json_obj` is the data 
    fetched from that URL as a JSON-format object. 
    :rtype: (string, dict or array)

    """
    encoded_params = urllib.urlencode(params)    
    url = query_url + encoded_params
    request = urllib2.Request(url, headers=headers)
    response = urllib2.urlopen(request)
    return (url, json.load(response))

您可以复制GoogleMaps软件包的源代码并尝试对其进行修补。

于 2013-10-05T12:33:30.803 回答