I have a problem that I am not sure how to solve. I want to iterate through a file where I want to convert the coordinates to the geolocation address.The code works fine but after it iteretes through a certain number of lines in the file the problem occurs.
from __future__ import print_function
from geopy.geocoders import Nominatim
from shapely.wkt import loads as load_wkt
from shapely.geometry import Point, Polygon
import io
import re
import ast
import time
geolocator = Nominatim()
with io.open('sample_test2.txt', encoding="utf-8") as f, io.open('sample_test3.txt', 'w',encoding="utf-8") as g:
for line in f:
m = re.sub(r'(70[0-9]+,).*', r'\1', line.rstrip())
z = re.sub(r'.*POINT \([0-9]+.[0-9]+ -[0-9]+.[0-9]+\)(.*)', r'\1', line.rstrip())
c = re.sub(r'.*POINT \(([0-9]+.[0-9]+) (-[0-9]+.[0-9]+)\).*', r'"\1, \2"', line.rstrip())
k = ast.literal_eval(c)
location = geolocator.reverse(k, timeout=60)
h = location.address
j = re.sub(r'.*, ([^,]+, [^,]+), [0-9]+, United.*', r'\1', h.rstrip())
print (m, j, z, file = g)
f.close()
g.close()
Now I read from some other questions that I should use time.sleep()
. Now I wanted to put it before the print
. The first time I run my code (without time.sleep()
) it came to around 1800 lines that he converted before getting this error:
raise GeocoderServiceError(message)
geopy.exc.GeocoderServiceError: HTTP Error 429: Too Many Requests
But now with or without the time.sleep()
it doesn't even do the first line it just breaks from the beginning with the error. Any idea what to do?
Looks like whatever webservice you are using has blocked you, most likely via your IP address. Wait for a while and then make sure that you are being "friendly" to the service, e.g., by inserting those sleeps.