我正在尝试对地址列表进行地理编码,并使用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')
我如何最有效地做到这一点?
为了有效地对大量位置进行地理编码,应查看Batch Geocoder API。