1

I'm using a Python script to send a list of addresses to the Google Geocoding API and parse the JSON results to give me a text list of coordinates. My problem is: For some addresses in the list, the API fails to return coordinates and instead returns the status "OVER_QUERY_LIMIT", but then it will continue to process more addresses successfully. For example, in a list of 200 addresses, I will get "OVER_QUERY_LIMIT" for roughly 40 or 60 of them, in no particular order, and the rest will be fine. But if I run the script again with the same list of addresses, some of those which failed the first time will return a result with coordinates like they should, with status "OK", but randomly different addresses in the list will fail. Does anyone have a clue why this would happen?

The script uses a loop to send each address as an API request one at a time, including a pause between each request to satisfy Google's limit on requests per second. And the total number of requests I'm sending should be well below Google's cap on free requests per day. So I don't believe I'm really going "over query limit" - also note that I am able to continue getting successful results after the ones that fail. So what could be making some addresses return this status at random?

Here's the script for anyone who's curious:

import urllib, json
import sys, time
import pprint

# specify input addresses as a list (for simplicity of demonstration):
addresses = [
   'address 1'
   'address 2'
   'address 3 etc.'
    ]
# I removed the real addresses for privacy, but you get the idea

for add in addresses:
    time.sleep(.1)
    prefix = 'https://maps.googleapis.com/maps/api/geocode/json?'
    data = urllib.urlencode({"address" : add})
    url = prefix+data
    gresp = urllib.urlopen(url)
    jresp = json.loads(gresp.read())
    if jresp['status'] == 'OK':
        lat = jresp['results'][0]['geometry']['location']['lat']
        lon = jresp['results'][0]['geometry']['location']['lng']
        print (str(lat)+"; "+str(lon))
    else:
        print jresp['status']

My results typically look something like this (for example):

33.782041; -84.4161232
33.8111927; -84.3640217
OVER_QUERY_LIMIT
33.7980347; -84.3691964
33.7396419; -84.3717205
33.7601601; -84.3955309
OVER_QUERY_LIMIT
33.7794269; -84.3675836
33.760549; -84.387061
33.80361; -84.3941337
33.814729; -84.3910812
OVER_QUERY_LIMIT
OVER_QUERY_LIMIT
33.7977534; -84.4076812
33.811963; -84.395412
OVER_QUERY_LIMIT
OVER_QUERY_LIMIT
OVER_QUERY_LIMIT
33.7682217; -84.3849992
33.8480993; -84.4284378
33.9229844; -84.3982854
  • download and use this python geocoder library pypi.python.org/pypi/geocoder/1.8.0 it will make your live way easier – ziggy Mar 6 '18 at 1:42
  • Thanks, I'll check out that library...but will it solve the question I asked? – Matt Leonard Mar 6 '18 at 2:44
  • Don't get me wrong, that library does look useful. Maybe I'll try using it to retrieve some coordinates and see if I still get the same issue... – Matt Leonard Mar 6 '18 at 2:51
  • i never ran into any issues with it before. I think google api has a frequency threshold. you may be only allowed to send 5 requested per second – ziggy Mar 6 '18 at 2:52
  • The time limit I've seen in Google documentation was 10 requests per second. Which is why my script includes a pause: time.sleep(.1) between each request, which should account for that. I've even tried increasing the length of that pause to .2 or .3 and I still got the same problem. – Matt Leonard Mar 6 '18 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.