1

from geopy.geocoders import Nominatim
import openpyxl
wb = openpyxl.load_workbook('#######.xlsx')
ws = wb.active
geolocator = Nominatim(timeout=60)

for i in range(2,1810):
    count1 = 0
    count2 = 1
    address = str(ws['B'+str(i)].value)
    city = str(ws['C'+str(i)].value)
    state = str(ws['D'+str(i)].value)
    zipc = str(ws['F'+str(i)].value)
    result = None
    iden1 = address + ' ' + city + ' ' + state
    iden2 = city + ' ' + zipc + ' ' + state
    iden3 = city + ' ' + state
    print(iden1, iden2, iden3)
    print(geolocator.geocode(iden2).address)
    try:
        location1 = geolocator.geocode(iden1)
    except:
        pass
    try:
        location2 = geolocator.geocode(iden2)
    except:
        pass
    try:
        location3 = geolocator.geocode(iden3)
    except:
        pass
    count = None
    try:
        county1 = str(location1.address)
        county1_list = county1.split(", ")
        #print(county1_list)
        for q in county1_list:
            if 'county' in q.lower():
                if count == None:
                    count = q
    except:
        pass
    try:
        county2 = str(location2.address)
        county2_list = county2.split(", ")
        #print(county2_list)
        for z in county2_list:
            if 'county' in z.lower():
                if count == None:
                    count = z
    except:
        pass
    try:
        county3 = str(location3.address)
        county3_list = county3.split(", ")
        #print(county3_list)
        for j in county3_list:
            if 'county' in j.lower():
                if count == None:
                    count = j
    except:
        pass
    print(i, count)
    #ws['E'+str(i)] = count
    if count == 50:
        #wb.save("#####" +str(count2) +".xlsx")
        count2 += 1
        count1 = 0

大家好,这段代码非常简单,使用 geopy 提取县名,使用 3 种不同的方法名称 iden1、iden2 和 iden3,它们是地址、城市、州和邮政编码的组合。这运行了大约 300 行,但开始重复同一个县,并且在重新启动脚本后,只是吐出 Nones。我输入了 print (geolocator.geocode(iden2).address) 行来查找错误并收到此错误消息。

回溯(最近一次通话最后):

文件“C:/Users/#####/Downloads/Web content/#####/####_county.py”,第 19 行,在 print(geolocator.geocode(iden2).address) 文件中“ C:\Users#####\AppData\Local\Programs\Python\Python36-32\lib\site-packages\geopy\geocoders\osm.py",第 193 行,地理编码 self._call_geocoder(url, timeout=超时),exact_one 文件“C:\Users#####\AppData\Local\Programs\Python\Python36-32\lib\site-packages\geopy\geocoders\base.py”,第 171 行,在 _call_geocoder 中引发 GeocoderServiceError (消息)geopy.exc.GeocoderServiceError: [WinError 10061] 无法建立连接,因为目标机器主动拒绝它

这个脚本以前可以工作,但现在不行。我的 IP 是否被阻止使用 goepy 的数据库或其他什么?谢谢你的帮助!

4

1 回答 1

0

看起来你正在达到他们的速率限制。他们似乎要求您将 API 请求限制为 1/秒。您可以在此处查看他们的使用政策,其中列出了使用其 API 的替代方案以及约束。

于 2017-06-07T01:32:27.340 回答