【发布时间】:2017-09-19 15:35:34
【问题描述】:
我使用一个程序来查询 Google 地理编码 API,并在坐标中搜索城市名称。我使用 time.sleep(1) 来确保我不超过每秒的请求限制。我也很确定我没有超过每日请求限制,并且我已经在我的帐户上启用了计费。
该代码前段时间可以运行,但现在我运行它时,代码返回如下错误:
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)
【问题讨论】:
标签: api ip geocoding google-geocoder google-geocoding-api