【问题标题】:Target machine refused connection目标机器拒绝连接
【发布时间】:2017-06-06 18:50:06
【问题描述】:

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) 行来查找错误并收到此错误消息。

Traceback(最近一次调用最后一次):

文件 "C:/Users/#####/Downloads/Web 内容/#####/####_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=timeout),exact_one 文件“C:\Users#####\AppData\Local\Programs\Python\Python36-32\lib\site-packages\geopy\geocoders\base.py” , 第 171 行,在 _call_geocoder 中 引发 GeocoderServiceError(message) geopy.exc.GeocoderServiceError: [WinError 10061] 无法连接 是因为目标机器主动拒绝了它

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

【问题讨论】:

  • 它又恢复了工作,我不知道为什么。
  • aaaa 而且它不再工作了哈哈!好吧,看起来我可以在它停止对我工作之前获得大约 300 个条目。必须与查找限制或类似的东西有关。

标签: python-3.x geopy nominatim


【解决方案1】:

您似乎达到了他们的速率限制。他们似乎要求您将 API 请求限制为 1/秒。您可以查看 here 以了解他们的使用政策,其中列出了使用其 API 的替代方案以及限制。

【讨论】:

  • 感谢您的回复!因此,如果我只是告诉脚本在每次循环迭代结束时等待,这应该可以解决问题,不是吗?我相信我对 API 的使用符合他们的使用政策。
  • 是的,如果您让脚本在发出另一个请求之前等待 1 秒,那么它应该可以工作。请注意,您的脚本将需要 1810 秒才能执行。您可能会考虑让它等待一秒钟多一点,以便确定。
猜你喜欢
  • 2016-04-02
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 2014-05-09
  • 1970-01-01
  • 2017-09-30
相关资源
最近更新 更多