Thursday, 27 August 2020

How to speed up my Google Map API requests in Python?


Hi I am a beginner in learning APII wanted to create a function that returns autocompleted address, using Google Maps API. I've tested them and when I return a single input of address - it works fine and quickly. I even check with two inputs of address and it works.But I guess my coding hasn't been designed for multiple inputs - I am guessing I have to bring another commend or to use other applications, assuming they are much robust.Could you please suggest an idea how I can improve them?? It will help me a lot. Thanks!#importing data import googlemaps mykey = 'Google credintials' gmaps = googlemaps.Client(key = mykey) import pandas as pd data = pd.read_csv(r"C:\Users\....csv") series = data['Billing Street'] #So this contains about 100 address inputs I want to check data = series.values.tolist() import sys #Google API import json import requests from urllib.parse import urlencode def get_autocomplete(address_name): #specify returned data type data_type = 'json' #provided by Google developers endpoint =f"https://maps.googleapis.com/maps/api/place/autocomplete/{data_type}" #input parameters then encode them params = {"input": address_name, "key": mykey} url_params = urlencode(params) url = f"{endpoint}?{url_params}" r = requests.get(url) if r.status_code not in range(200, 299): return sys.exit("Error message") return r.json() list = [] for i in range(len(data)): output = get_autocomplete(data[i]) if (output["status"] != "ZERO_RESULTS"): list.append(output["predictions"][0]['description']) else: list.append("ZERO_RESULTS") print(list)

No comments:

Post a Comment