2017-09-19 91 views
0

我使用程序查询Google地理编码API,并搜索城市名称的坐标。我使用time.sleep(1)以确保我没有超过每秒的请求限制。我也非常肯定,我没有超出每日限制的要求,并且已经在我的帐户中启用了结算功能。Google地理编码API已阻止我的IP地址

的代码工作前一段时间,但现在我运行它,代码返回这样的错误:

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

我去了在浏览器的API链接,它要求我输入识别字符,说:“我们的系统已经检测到来自您的计算机网络的异常流量,该页面检查是否确实是您发送请求,而不是机器人。”

我不认为我的代码有什么恶意,我不知道我做了什么错误的代码。

这里是我的代码:

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) 

回答

0

好像你总是期待的研制成功效应初探。

而不是

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

可以使用

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

使用最后一个你永远不会得到,当你正在阅读的字典(JSON)例外,即使字典没有你期待的关键。