Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在web抓取中将geocodeapi键与python集成_Python_Api_Web Scraping_Geocode - Fatal编程技术网

如何在web抓取中将geocodeapi键与python集成

如何在web抓取中将geocodeapi键与python集成,python,api,web-scraping,geocode,Python,Api,Web Scraping,Geocode,我正在尝试使用谷歌地理编码api来获取一个地方的纬度和经度。 我还创建了GeocodeAPI密钥并将其包含在程序中,但我认为在代码中集成该密钥时存在错误 我正在python中使用web抓取。 我还包括了urllib、json和ssl模块 我创建了字典来存储api键和位置地址,并创建了urllib.parse.urlencode()来像浏览器那样对url进行编码 以下是示例代码: import json import urllib.request,urllib.parse,urllib.error

我正在尝试使用谷歌地理编码api来获取一个地方的纬度和经度。 我还创建了GeocodeAPI密钥并将其包含在程序中,但我认为在代码中集成该密钥时存在错误

我正在python中使用web抓取。 我还包括了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)


检查Google云控制台中是否启用了地理编码API。此外,您还需要有一个计费帐户才能使用此API。谢谢,启用API后,它的计费错误。
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.