繁体   English   中英

为什么我的Google Maps API请求填充如此缓慢?

[英]Why are my Google Maps API requests populating so slowly?

我正在尝试在python3中编写脚本(使用Jupyter笔记本),该脚本使用Google Maps Geocoding API查询每个地址的坐标。 Google施加的限制是每秒50个请求,但是我的代码运行得慢得多(也许20分钟才能处理1000行)。

我使用python请求库进行api调用,并使用pandas执行数据转换(即将json输出解析为所需的格式)。 我会分批输入我的输入,以避免在发生意外异常时丢失所有输出。

我的问题是,为什么每次迭代都花这么长时间? 我对此很陌生,因此很难确定是什么使我放慢了脚步。

程式码片段:

def populate_coordinates(list_of_addresses, api = api_key):
    filled_table = pd.DataFrame({"Pickup address": [],
                                 "Status": [],
                                 #"Postal": [],
                                 "Formatted address": [],
                                 "Lat": [],
                                 "Long": [],
                                 "Location type": []})
    count = 0
    requests.packages.urllib3.disable_warnings() 
    for address in list_of_addresses:
        data = {"address": address,
                "key": api,
                "region": "us"}
        response = requests.get(endpoint, params = data, verify = False)
        response_json = response.json()
        if response_json["status"] == "OK":
            #address_comp = pd.DataFrame(response_json["results"][0]["address_components"])
            #postal = address_comp[address_comp["types"]=="postal_code"]["long_name"][0]
            fmt_add = response_json["results"][0]["formatted_address"]
            lat = response_json["results"][0]["geometry"]["location"]["lat"]
            long = response_json["results"][0]["geometry"]["location"]["lng"]
            loc_type =  response_json["results"][0]["geometry"]["location_type"]

            filled_table = filled_table.append({"Pickup address": address,
                                                "Status": response_json["status"],
                                                #"Postal": postal,
                                                "Formatted address": fmt_add,
                                                "Lat": lat,
                                                "Long": long,
                                                "Location type": loc_type}, ignore_index = True)
        else:
            filled_table = filled_table.append({"Pickup address": address,
                                                "Status": response_json["status"],
                                                #"Postal": None,
                                                "Formatted address": None,
                                                "Lat": None,
                                                "Long": None,
                                                "Location type": None }, ignore_index = True)
        count+=1
        print ("Processing: {} of {} addresses".format(count, len(list_of_addresses)))
    return filled_table

这是批处理部分(如果相关):

batch_sz = 2000
num_batch = 0
num_entries = 0

results = pd.DataFrame({"Pickup address": [],
                        "Status": [],
                        "Formatted address": [],
                        "Lat": [],
                        "Long": [],
                        "Location type": []})
while num_entries<len(addresses):
    num_batch+=1
    batch_complete = populate_coordinates(addresses[num_entries:num_entries+batch_sz])
    num_entries+=batch_sz
    results = pd.concat([results, batch_complete], sort = False)

欢迎其他提示/建议!

这可能需要很长时间,因为当您使用任何API方法请求数据时,您都会发出HTTP请求以JSON格式检索响应,但这当然不是原因。 我也尝试过几次Google Map API,但我认为尝试许多请求可能会使速度变慢。因为有时此类网站会在有限的时间内为您提供有限的请求。

解析过程也可能需要时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM