微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在网络抓取中将地理编码api密钥与python集成

如何解决如何在网络抓取中将地理编码api密钥与python集成

我正尝试使用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 举报,一经查实,本站将立刻删除。