【发布时间】: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