编程语言
首页 > 编程语言> > HTTP错误429:python geopy请求过多

HTTP错误429:python geopy请求过多

作者:互联网

我有一个我不确定如何解决的问题.我想遍历要将坐标转换为地理位置地址的文件.代码可以正常工作,但是遍历文件中的某些行后会出现问题.

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().现在我想把它放在印刷品上.我第一次运行我的代码(没有time.sleep())时,他遇到了大约1800行,在发生此错误之前,他将其转换为:

    raise GeocoderServiceError(message)
geopy.exc.GeocoderServiceError: HTTP Error 429: Too Many Requests

但是现在不管有没有time.sleep(),它甚至都不会做第一行,只是从错误的开头就中断了.知道该怎么办吗?

解决方法:

似乎您正在使用的任何Web服务都已阻止您,很可能是通过您的IP地址阻止了您.请稍等片刻,然后确保您对服务“友好”,例如,插入那些睡眠.

标签:geopy,python,python-requests,http-status-code-429
来源: https://codeday.me/bug/20191012/1900856.html