解决geopy geocoders ConfigurationError 问题

已解决!geopy geocoders 运行代码块报错问题。

from geopy.geocoders import Nominatim

# 初始化api
geolocator = Nominatim(user_agent="my-application")

# 邮编
place = "40.056793 116.305811"
location = geolocator.reverse(place)  # 纬度在前,经度在后

# 获得数据
data = location.raw
print(data)
loc_data = data['display_name'].split()
print("Full Location:", loc_data)
print("Zip code : ", loc_data[-2])

出现报错: geopy.exc.ConfigurationError: Using Nominatim with default or sample user_agent “geopy/2.4.1” is strongly discouraged, as it violates Nominatim’s ToS https://operations.osmfoundation.org/policies/nominatim/ and may possibly cause 403 and 429 HTTP errors. Please specify a custom user_agent with Nominatim(user_agent="my-application") or by overriding the default user_agent: geopy.geocoders.options.default_user_agent = "my-application".

解决方法: 打开 anaconda 路径下文件:anaconda\envs\python38\Lib\site-packages\geopy\geocoders\nominatim.py
(Add a # before each line in raise ConfigurationError( … ) , and add a line that says pass right above.)
第105行修改为,注释掉raise ConfigurationError( … ),改为pass。

if (self.domain == _DEFAULT_NOMINATIM_DOMAIN
                and self.headers['User-Agent'] in _REJECTED_USER_AGENTS):
            pass
            # raise ConfigurationError(
            #     'Using Nominatim with default or sample `user_agent` "%s" is '
            #     'strongly discouraged, as it violates Nominatim\'s ToS '
            #     'https://operations.osmfoundation.org/policies/nominatim/ '
            #     'and may possibly cause 403 and 429 HTTP errors. '
            #     'Please specify a custom `user_agent` with '
            #     '`Nominatim(user_agent="my-application")` or by '
            #     'overriding the default `user_agent`: '
            #     '`geopy.geocoders.options.default_user_agent = "my-application"`.'
            #     % self.headers['User-Agent']
            # )

即可运行成功:

{'place_id': 194340070, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright', 'osm_type': 'way', 'osm_id': 1186225518, 'lat': '40.056787094612076', 'lon': '116.30581209681867', 'class': 'highway', 'type': 'secondary', 'place_rank': 26, 'importance': 0.10000999999999993, 'addresstype': 'road', 'name': '西二旗北路', 'display_name': '西二旗北路, 海淀区, 北京市, 102208, 中国', 'address': {'road': '西二旗北路', 'city': '海淀区', 'state': '北京市', 'ISO3166-2-lvl4': 'CN-BJ', 'postcode': '102208', 'country': '中国', 'country_code': 'cn'}, 'boundingbox': ['40.0567774', '40.0601057', '116.3057599', '116.3174453']}
Full Location: ['西二旗北路,', '海淀区,', '北京市,', '102208,', '中国']
Zip code :  102208,
  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值