0

I use a program to query the Google Geocoding API, and search coordinates for city names. I use time.sleep(1) to make sure I do not exceed the request limit per second. I am also pretty sure that I have no exceeded the daily limited of request and I have enabled billing on my account.

The code worked a while ago, but now as I run it, the code returns an error like this:

 raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I went to the API link in the browser, and it asks me to type in identification characters, saying "Our systems have detected unusual traffic from your computer network. This page checks to see if it's really you sending the requests, and not a robot.".

I don't think my code is anything malicious, and I wonder what I have done wrong with the code.

Here is my code:

 import urllib.parse
import requests
import pandas as pd
import time


main_api = 'https://maps.googleapis.com/maps/api/geocode/json?'
api_key = '&key = **********'
state = "washington,"
country = "US"

file = r'C:\Users\rin\xy.xlsx'
xl_workbook = pd.ExcelFile(file)
df = xl_workbook.parse("xy")
components = 'components=country:US|state:washington'

city_name = df['city_name'].tolist()

for x in post_office:
    address = x
    print(">>>>>" + x + "<<<<<")

    url = main_api + urllib.parse.urlencode({'address': address}) + state + country + api_key + components
    print(url)
    json_data = requests.get(url).json()

    json_status = json_data['status']
    print('API status: ' + json_status + '\n')

    city = json_data['results'][0]['address_components'][0]['long_name']
    state = json_data['results'][0]['address_components'][2]['long_name']
    if x == city and state == "Washington":
        print("MATCH:OK" + '\n')
    if x == city and state == "Oregon":
        print("ooooooooooo")
    elif x != city_name:
        print("WN")

    if json_status == "OK":

        formatted_address = json_data['results'][0]["formatted_address"]
        print()
        print(formatted_address)

        LATITUDE = json_data['results'][0]["geometry"]["location"]["lat"]
        LONGITUDE = json_data['results'][0]["geometry"]["location"]["lng"]
        print(LATITUDE)
        print(LONGITUDE)

        print(">>>>>>>END<<<<<<")
        print('\n')

    time.sleep(1)
iuiu
  • 1
  • 2

2 Answers2

0

It seems like you are always expecting the sucess reponse.

Instead of

LONGITUDE = json_data['results'][0]["geometry"]["location"]["lng"]

you can use

LONGITUDE = json_data['results'][0]["geometry"]["location"].get("lng", None)

Using the last one you never get exception when you are reading a dict (JSON), even if the dict doesn't have a key that you expecting.

0

It seems that the json you are receiving does not match the deserializer standard. Can you paste the content of the request you are receiving ?

Have a look at that Python error load JSON code of google API