【发布时间】:2016-06-25 18:31:59
【问题描述】:
我有一个不知道如何解决的问题。我想遍历一个我想将坐标转换为地理位置地址的文件。代码工作正常,但是在它遍历文件中的一定数量的行之后,问题就出现了。
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 行代码:
raise GeocoderServiceError(message)
geopy.exc.GeocoderServiceError: HTTP Error 429: Too Many Requests
但是现在不管有没有time.sleep(),它甚至都不会做第一行,它只是从头开始就出现错误。知道该怎么做吗?
【问题讨论】:
-
这段代码和我放一个 time.sleep(0.1) 有什么区别,就像我读的代码一样,它限制为每秒 10 次,而 time.sleep(0.1) 我也这样做还是?
标签: python python-requests geopy http-status-code-429