You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google Maps Places Nearby API获取超20家餐厅失败求助

问题分析与解决办法

核心问题原因

  1. API分页上限限制:Google Places Nearby Search API每个查询(单个位置+搜索条件)最多返回60条结果(3页,每页20条),即使该区域实际商家超过60家,也无法获取更多。
  2. 重复过滤参数:代码中同时使用了keyword和name两个参数,双重过滤会缩小结果范围,导致返回的商家数量减少。
  3. page_token生效延迟不足:time.sleep(2)可能不足以让新生成的page_token生效,导致请求失败或返回空结果。
  4. 重复数据未去重:不同位置的搜索范围重叠,同一个商家被多次抓取,最终统计的有效唯一数据量减少。

针对性解决步骤

  • 优化搜索参数:删除重复的过滤参数,仅保留keyword或结合type参数扩大搜索范围。比如用type='restaurant'明确搜索餐厅类型,再搭配keyword='كشري'。
  • 增加延迟时间:将time.sleep(2)调整为time.sleep(3)或更长,确保page_token完全生效。
  • 减少搜索半径或拆分位置:如果单个位置半径内商家超过60家,缩小radius值,或者将大区域拆分为更多细分位置点,覆盖原区域,从而获取更多结果。
  • 添加去重逻辑:基于商家的place_id对结果去重,避免重复统计同一商家。

修改后的代码示例

import time
import googlemaps

with open('apikey.txt') as f:
    api_key = f.readline().strip()  # 去除换行符
    f.close()

gmaps = googlemaps.Client(api_key)
locations = [(30.0416162646183, 31.187637297709912),(30.038828662868447, 31.21133524457125),(29.848956883337507, 31.334386571579085),(30.047845819479956, 31.262317130706496),(30.05312112490655, 31.24665544474578),(30.044482967408886, 31.23572953125353),(30.02023034028819, 31.176992671570066),(30.055592085960892, 31.18411299557052),(30.0512387364253, 31.20328697618034),(30.027741592767295, 31.174344307489818),(30.043337503059586, 31.17587613443309),(30.049286828183856, 31.181250916540794),(30.043423144171197, 31.187248209629644),(30.040934096091647, 31.183299998037857),(30.038296379882215, 31.189823130232988),(29.960107152991863, 31.250999388927262) , (29.83911392727571, 31.30468827173587) , (29.842752004034566, 31.332961535887694)]
search_string = "كشري"
distance = 10000
kosharies = []
seen_place_ids = set()  # 用于去重的集合

for location in locations:
    # 优化参数:使用type明确餐厅类型,仅保留keyword
    response = gmaps.places_nearby(location=location, keyword=search_string, type='restaurant', radius=distance)
    
    # 处理第一页结果,去重
    for result in response.get('results', []):
        place_id = result.get('place_id')
        if place_id not in seen_place_ids:
            seen_place_ids.add(place_id)
            kosharies.append(result)
    
    next_page_token = response.get('next_page_token')
    
    while next_page_token:
        time.sleep(3)  # 增加延迟时间
        another_response = gmaps.places_nearby(
            location=location, 
            keyword=search_string, 
            type='restaurant', 
            radius=distance,
            page_token=next_page_token
        )
        
        # 处理分页结果,去重
        for result in another_response.get('results', []):
            place_id = result.get('place_id')
            if place_id not in seen_place_ids:
                seen_place_ids.add(place_id)
                kosharies.append(result)
        
        next_page_token = another_response.get('next_page_token')

print(f"最终获取到的唯一餐厅数量:{len(kosharies)}")

额外注意事项

  • 检查API配额:确保你的Google Cloud账号的Places API配额足够支撑多次请求,避免因配额耗尽导致结果中断。
  • 拆分大区域:如果某个位置半径10km内商家远超60家,可以将distance缩小到5km,同时在原区域内增加更多位置点,确保覆盖所有目标区域,从而获取更多结果。

内容的提问来源于stack exchange,提问作者Ismail Awad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 01:50:29