2017-06-06 38 views
0

目标机器拒绝连接

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。我把行打印(geolocator.geocode(iden2)。地址)找到错误,并得到这个错误信息。

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

File "C:/Users/#####/Downloads/Web content/#####/####_county.py", line 19, in print(geolocator.geocode(iden2).address) File "C:\Users#####\AppData\Local\Programs\Python\Python36-32\lib\site-packages\geopy\geocoders\osm.py", line 193, in geocode self._call_geocoder(url, timeout=timeout), exactly_one File "C:\Users#####\AppData\Local\Programs\Python\Python36-32\lib\site-packages\geopy\geocoders\base.py", line 171, in _call_geocoder raise GeocoderServiceError(message) geopy.exc.GeocoderServiceError: [WinError 10061] No connection could be made because the target machine actively refused it

这个剧本是工作之前,但现在没有。我的IP被阻止使用goepy的数据库或其他东西?谢谢你的帮助!

+0

它又恢复了工作,我不知道为什么。 –

+0

aaaaand它不再工作哈哈!好吧,看起来在我停止工作之前,我可以获得大约300个参赛作品。必须与查询限制或类似的事情有关。 –

回答

0

它看起来像是你的速率限制。他们似乎要求您将API请求限制为1秒。你可以看看他们的使用政策here,他们列出了使用他们的API和限制的替代方案。

+0

感谢您的回应!所以如果我只是告诉脚本在每次循环迭代结束时等待,这应该解决问题,不是吗?我相信我对API的使用符合其使用策略。 –

+0

是的,如果您在创建另一个请求之前已经等待了1秒,那么它应该可以工作。请注意,您的脚本需要1810秒才能执行。只是为了确定,你可能会考虑让它等一会儿。 – klvs

相关问题