geopy调用Baidu出现geopy.exc.GeocoderAuthenticationFailure: Authentication Failure

问题描述

pip install geopy安装了版本为1.21.0的geopy

from geopy.geocoders import Baidu

geolocator = Baidu(api_key='我的ak')
location = geolocator.geocode('西北农林科技大学')
print(location.address)
print((location.latitude, location.longitude))
print(location.raw)

报错geopy.exc.GeocoderAuthenticationFailure: Authentication Failure

追根溯源

geopy构造的url

https://api.map.baidu.com/geocoder/v2/?ak=我的ak&output=json&address=西北农林科技大学

访问得到结果

{"status":200,"message":"APP不存在,AK有误请检查再重试"}

使用的Baidu API仍然是V2,但现在的版本已经是V3了,正确的调用应该是

http://api.map.baidu.com/geocoding/v3/?address=西北农林科技大学&output=json&ak=我的ak

关键点就是geocoder/v2改成geocoding/v3

解决方案

修改C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Lib\site-packages\geopy\geocoders\baidu.py

api_path = '/geocoding/v3'
发布了248 篇原创文章 · 获赞 89 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/lly1122334/article/details/104314345