1 year ago

#191964

test-img

Jessica Garcia

Geocoding Error via Python and Google's Geocoding API

I am in need of some help at the moment with some research that I am working on in python. I cannot get this Geocoding API by google to work for some reason and I am not understanding the error code despite Googling that stuff. Note: I have a dataframe that I converted into a dictionary to pass into this documentation for the Geocoding API.

The code:

API_KEY = 'private'  

# appending all addresses in radius_0 to the list titled addresses  

addresses = [] 
for each_dict in final_dict:     
address = final_dict['Location']     
addresses.append(address) 
print(addresses)  

# parameters  

params = {     'key': API_KEY,      'address': addresses }  

# following google documentation with the url provided for the API request  

base_url = 'https://maps.googleapis.com/maps/api/geocode/json?'  

# to see print(response) 

response = requests.get(base_url, params=params).json()  

# accessing the keys of the dictionary titled 

response  response.keys()  
if response['status'] == 'OK':      
geometry = response['results'][0]['geometry']     
lat = geometry['location']['lat']     
lon = geometry['location']['lng'] 
print((lat, lon)) 

# Error Code: 
    HTTPError                                 Traceback (most recent call last)
/var/folders/s6/zdp5jv2j74l4k3nty81q0wrm0000gn/T/ipykernel_40787/2892721893.py in <module>
     25 
     26 response = requests.get(base_url, params=params)
---> 27 response.raise_for_status()  # raises exception when not a 2xx response
     28 if response.status_code != 204:
     29     return response.json()

~/opt/anaconda3/lib/python3.9/site-packages/requests/models.py in raise_for_status(self)
    951 
    952         if http_error_msg:
--> 953             raise HTTPError(http_error_msg, response=self)
    954 
    955     def close(self):

HTTPError: 400 Client Error: Bad Request for url:

Thank you very much and any help or tips are extremely appreciated!

python

geocoding

google-geocoding-api

0 Answers

Your Answer

Accepted video resources