gpt4 book ai didi

python - Python geopy 地理编码器中的超时错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:18:31 27 4
gpt4 key购买 nike

我是一个相对较新的 Python 用户,我正在尝试使用一个函数来使用“geopy”模块返回城市和国家/地区的纬度和经度。我有错误,因为我的城市拼错了,我设法 catch 了。我现在遇到的麻烦是我遇到超时错误。我读过这个问题Geopy: catch timeout error并相应地调整了我的超时参数。 但是,在我收到超时错误之前,它现在运行了不同的时间长度。我试过在更快的网络上运行它,它在某种程度上起作用了。问题是我需要对 100k 行执行此操作,并且它在超时之前迭代的最大行数是 20k。非常感谢任何有关如何解决此问题的帮助/建议。

import os
from geopy.geocoders import Nominatim
os.getcwd() #check current working directory
os.chdir("C:\Users\Philip\Documents\HDSDA1\Project\Global Terrorism Database")

#import file as a csv
import csv
gtd=open("gtd_original.csv","r")
csv_f=csv.reader(gtd)
outf=open("r_ready.csv","wb")
writer=csv.writer(outf,dialect='excel')
for row in csv_f:
if row[13] in ("","NA") or row[14] in ("","NA"):
lookup = row[12] + "," + row[8] # creates a city,country
geolocator = Nominatim()
location = geolocator.geocode(lookup, timeout = None) #looks up the city/country on maps
try:
location.latitude
except:
lookup = row[8]
location = geolocator.geocode(lookup)
row[13] = location.latitude
row[14] = location.longitude
writer.writerow(row)
gtd.close()
outf.close()

最佳答案

我希望您超出了 Nominatim 服务 ( http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy ) 的使用政策。尝试在请求之间暂停 1 秒并缓存结果,可能有很多重复项。

sleep 部分:

from time import sleep
### your code
row[14] = location.longitude
sleep(1) # after last line in if

缓存:

coords = {}
if coords.has_key([row[8], row[12] ]):
row[13] , row[14] = coords[ [ row[8], row[12] ] ]
else:
#geolocate

更新

性能:1 个请求/秒 --> 3600 个请求/小时 --> 36K 个请求/10 小时

import os
from time import sleep
from geopy.geocoders import Nominatim
os.getcwd() #check current working directory
os.chdir("C:\Users\Philip\Documents\HDSDA1\Project\Global Terrorism Database")

#import file as a csv
import csv
gtd=open("gtd_original.csv","r")
csv_f=csv.reader(gtd)
outf=open("r_ready.csv","wb")
writer=csv.writer(outf,dialect='excel')
coords = {}
for row in csv_f:
if row[13] in ("","NA") or row[14] in ("","NA"):
lookup = row[12] + "," + row[8] # creates a city,country

if coords.has_key( (row[8], row[12]) ): ## test if result is already cached
row[13] , row[14] = coords[ (row[8], row[12]) ]
else:
geolocator = Nominatim()
location = geolocator.geocode(lookup, timeout = None) #looks up the city/country on maps
try:
location.latitude
except:
lookup = row[8]
location = geolocator.geocode(lookup)
row[13] = location.latitude
row[14] = location.longitude
coords[ (row[8], row[12]) ] = (row[13] , row[14]) # cache the new coords
sleep(1) # sleep for 1 sec (required by Nominatim usage policy)

writer.writerow(row)
gtd.close()
outf.close()

关于python - Python geopy 地理编码器中的超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30218394/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com