Geopy: catch timeout error

前端 未结 3 1541
难免孤独
难免孤独 2020-12-09 03:22

I am using geopy to geocode some addresses and I want to catch the timeout errors and print them out so I can do some quality control on the input. I am putting the geocode

3条回答
  •  半阙折子戏
    2020-12-09 04:13

    Try this:

    from geopy.geocoders import Nominatim
    from geopy.exc import GeocoderTimedOut
    
    my_address = '1600 Pennsylvania Avenue NW Washington, DC 20500'
    
    geolocator = Nominatim()
    try:
        location = geolocator.geocode(my_address)
        print(location.latitude, location.longitude)
    except GeocoderTimedOut as e:
        print("Error: geocode failed on input %s with message %s"%(my_address, e.message))
    

    You can also consider increasing the timeout on the geocode call you are making to your geolocator. In my example it would be something like:

    location = geolocator.geocode(my_address, timeout=10)
    

    or

    location = geolocator.geocode(my_address, timeout=None)
    

提交回复
热议问题