错误(429)在Python中使用geopy进行地理编码时,请求过多 [英] error (429) Too Many Requests while geocoding with geopy in Python

查看:236
本文介绍了错误(429)在Python中使用geopy进行地理编码时,请求过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个约有2万行的Pandas数据帧,我正在尝试按地址列将其地理编码为经纬度坐标.

I have a Pandas dataframe with ~20k rows, and I am trying to geocode by address column into lat/long coordinates.

我如何使用time.sleep()或其他功能来阻止我现在得到的Too Many Requests 429 error的OSM Nominatim?

How do I use time.sleep() or maybe other function to stop OSM Nominatim from Too Many Requests 429 error that I am getting now?

这是我用于此的代码:

from geopy.geocoders import Nominatim
from geopy.distance import vincenty

geolocator = Nominatim()
df['coord'] = df['address'].apply(geolocator.geocode).apply(lambda x: (x.latitude, x.longitude))
df.head()

提前谢谢!

推荐答案

geopy,因为1.16.0包含一个RateLimiter类,该类通过在查询之间添加延迟并重试失败的方法提供了一种方便的方式来处理Too Many Requests 429 error请求.

geopy since 1.16.0 includes a RateLimiter class which provides a convenient way to deal with the Too Many Requests 429 error by adding delays between the queries and retrying the failed requests.

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="specify_your_app_name_here")

from geopy.extra.rate_limiter import RateLimiter
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)

df['coord'] = df['address'].apply(geocode).apply(lambda location: (location.latitude, location.longitude))
df.head()

文档: https://geopy.readthedocs.io/en /1.16.0/#usage-with-pandas

这篇关于错误(429)在Python中使用geopy进行地理编码时,请求过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆