HTTP错误429:python geopy请求过多 [英] HTTP Error 429: Too Many Requests by python geopy
问题描述
我有一个我不确定如何解决的问题.我想遍历要将坐标转换为地理位置地址的文件.代码可以正常工作,但遍历文件中的特定行数后会出现问题.
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()
现在,我从其他一些问题中读到我应该使用time.sleep()
.现在,我想将其放在print
之前.第一次运行我的代码(没有time.sleep()
)时,他遇到了大约1800行,然后才转换为错误:
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
但是现在不管有没有time.sleep()
,它甚至都没有做第一行,只是从错误的开头就中断了.知道该怎么办吗?
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?
推荐答案
看起来您正在使用的任何Web服务都已阻止了您,很可能是通过您的IP地址阻止了您.等待一会儿,然后例如通过插入那些睡眠来确保您对服务友好".
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.
这篇关于HTTP错误429:python geopy请求过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!