超出最大重试次数:对多个地址进行地址解析并写入excel

问题描述 投票:0回答:1

我正在尝试对地址列表进行地理编码,并使用HERE API将其附加到excel文件中,但是我遇到了错误,超过了最大重试次数。

这是我当前的代码:

def geocode(location):
    # api-endpoint 
    URL = "https://geocoder.ls.hereapi.com/6.2/geocode.json"

    # defining a params dict for the parameters to be sent to the API 
    PARAMS = {'searchtext':location, 'gen':9,'apiKey':"s"} 

    # sending get request and saving the response as response object 
    r = requests.get(url = URL, params = PARAMS) 

    if response.status_code == 200:
    # extracting data in json format 
    out = r.json() 

    try:

        area =   out['Response']['View'][0]['Result'][0]['Location']['Address']['County'] 
        city =   out['Response']['View'][0]['Result'][0]['Location']['Address']['City'] 
        zip  =   out['Response']['View'][0]['Result'][0]['Location']['Address']['PostalCode'] 

    except IndexError :

        area = ""
        city = ""
        zip = ""



    return out,area,city,zip

这是我的循环:

# Loop thru addresses

i = 1
for primary in primary_address:
   i = i + 1
   out,area,city,zip = geocode(primary) 
   sheet.cell(row=i, column=first_empty_col).value = area 
   sheet.cell(row=i, column=first_empty_col + 1).value = city
   sheet.cell(row=i, column=first_empty_col + 2).value = zip

book.save('file.xlsx')

我如何最有效地做到这一点?

python pandas openpyxl here-api
1个回答
0
投票

为了有效地对大量位置进行地理编码,应查看Batch Geocoder API

© www.soinside.com 2019 - 2024. All rights reserved.