问题描述
我正尝试使用Google地理编码api获取某个地方的经度和纬度。 我还创建了geocode api密钥并将其包含在程序中,但是我认为它在代码中的集成存在错误。
我在python中使用网络抓取。 我还包括urllib,json和ssl模块。
我已经创建了字典来存储api键和位置地址,并创建了urllib.parse.urlencode()来像浏览器一样对url进行编码。
这是示例代码:
import json
import urllib.request,urllib.parse,urllib.error
import ssl
api_key=False
api_key ='AIza '
if api_key is False:
api_key=42
serviceurl='http://py4e-data.dr-chuck.net/json?'
else:
serviceurl='https://maps.googleapis.com/maps/api/geocode/json?'
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
while True:
address=input('Enter location:')
if len(address)<1: break
params=dict()
params['address']=address
if api_key is not False: params['key']=api_key
url=serviceurl+urllib.parse.urlencode(params)
print('Retrieving',url)
uh = urllib.request.urlopen(url,context=ctx)
data = uh.read().decode()
print('Retrieved',len(data),'characters')
try:
js = json.loads(data)
except:
js = None
if not js or 'status' not in js or js['status'] != 'OK':
print('==== Failure To Retrieve ====')
print(data)
continue
print(json.dumps(js,indent=4))
lat = js['results'][0]['geometry']['location']['lat']
lng = js['results'][0]['geometry']['location']['lng']
print('lat',lat,'lng',lng)
location = js['results'][0]['formatted_address']
print(location)
我得到的输出是:
Retrieved 130 characters
==== Failure To Retrieve ====
{
"error_message" : "This API project is not authorized to use this API.","results" : [],"status" : "REQUEST_DENIED"
}
I have created the api key for geocode. But output is request denial.
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)