2017-06-06 40 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

相關問題