geopy: Nominatim HTTP Error 403: Forbidden for default User-Agent

Nominatim API seems to block the default User-Agent: Python-urllib/3.5 now: HTTP Error 403: Forbidden.

Looking at the code the default User-Agent should be geopy/1.11.0? I’ve tried to set own user-agent by: Nominatim(user_agent='MyApp/1.2.3') but the request has still the user agent User-Agent: Python-urllib/3.5 and it fails with HTTP 403. So, the user-agent param does not work properly.

Edit: duplicate to #185 and probably solved by #184. Leaving this opened, since the Nominatim has started to block these requests just recently. Just to prevent other folks to duplicate the issue like me.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 15 (1 by maintainers)

Commits related to this issue

Most upvoted comments

@edwin34 python2

from geopy.geocoders import Nominatim
import urllib2

def get_geolocator():
    geolocator = Nominatim()

    requester = geolocator.urlopen

    def requester_hack(req, **kwargs):
        req = urllib2.Request(url=req, headers=geolocator.headers)
        return requester(req, **kwargs)

    geolocator.urlopen = requester_hack

    return geolocator

location = get_geolocator().reverse("52.509669, 13.376294")
address = location.address

python3

from geopy.geocoders import Nominatim
from urllib.request import Request

def get_geolocator():
    geolocator = Nominatim()

    requester = geolocator.urlopen

    def requester_hack(req, **kwargs):
        req = Request(url=req, headers=geolocator.headers)
        return requester(req, **kwargs)

    geolocator.urlopen = requester_hack

    return geolocator

location = get_geolocator().reverse("52.509669, 13.376294")
address = location.address

Note: temporary hack to overcome this issue:

    def nominatim_hack(geocoder: Nominatim):
        """HACK to geopy bugs #262 and #185"""
        requester = geocoder.urlopen

        def requester_hack(req, **kwargs):
            req = Request(url=req, headers=geocoder.headers)
            return requester(req, **kwargs)

        geocoder.urlopen = requester_hack

And use it:

locator = Nominatim()
locator = nominatim_hack(locator)
location = locator.reverse("52.509669, 13.376294")

Yeah, the reason why these hacks have broken is because the req argument of the geolocator.urlopen might be a Request instance rather than a string url.

This problem has been fixed in 3395aee and it’s already released in 1.12.0, so these hacks are not needed anymore. Nominatim now works out of the box again, as well as supplying a custom user_agent.

I’m closing this.

from geopy.geocoders import Nominatim

def nominatim_hack(geocoder: Nominatim): requester = geocoder.urlopen

def requester_hack(req, **kwargs): req = Request(url=req, headers=geocoder.headers) return requester(req, **kwargs)

geocoder.urlopen = requester_hack

locator = Nominatim() locator = nominatim_hack(locator) location = locator.reverse(“52.509669, 13.376294”) print (location.address)

This doesn’t work for me too, please help me to fix this. It shows the following error:- File “jio.py”, line 3 def nominatim_hack(geocoder: Nominatim): SyntaxError: invalid syntax