【问题标题】:Google Geocoding API Blocked My IP Address谷歌地理编码 API 阻止了我的 IP 地址
【发布时间】: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


    【解决方案1】:

    您似乎一直在期待成功的响应。

    代替

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

    你可以使用

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

    使用最后一个,当您读取 dict (JSON) 时,您永远不会遇到异常,即使该 dict 没有您期望的键。

    【讨论】:

      【解决方案2】:

      您收到的 json 似乎与反序列化器标准不匹配。 你能粘贴你收到的请求的内容吗?

      看看Python error load JSON code of google API

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-17
        • 1970-01-01
        • 1970-01-01
        • 2016-03-13
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多