如何用Python结合Google Places API获取指定地址的加油站Place ID?
获取地址内特定子场所(76加油站)的Place ID及评论数据
问题分析
你当前调用findplacefromtext接口仅传入地址,API会优先匹配地址对应的建筑主体(Place ID: ChIJ9Scljph-hYARAU-0jfBmjqY),而该建筑本身没有评论数据。网页版Google Maps显示的“At this place”板块里的76加油站是该地址下的子场所,需要针对性获取它的Place ID。
解决方案
方式一:精准关键词搜索(推荐)
直接在查询文本中加入“76 gas station”,让API直接定位目标加油站,避免匹配到建筑主体:
import requests API_KEY = "你的API密钥" # 加入加油站名称,提升搜索精准度 search_text = '1101 E FLORENCE AVE, LOS ANGELES, CALIFORNIA 90001 76 gas station' search_text_encoded = search_text.replace(' ', '%20') # 调用findplacefromtext接口,同时获取name和place_id用于验证匹配结果 url = f"https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input={search_text_encoded}&inputtype=textquery&fields=place_id,name&key={API_KEY}" response = requests.get(url).json() # 筛选出名称含76的场所Place ID place_ids = [c['place_id'] for c in response.get('candidates', []) if '76' in c.get('name', '')] # 获取评论数据并处理 for place_id in place_ids: details_url = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={place_id}&key={API_KEY}&fields=reviews" details_response = requests.get(details_url).json() # 增加错误判断,避免无评论时抛出异常 if 'result' in details_response and 'reviews' in details_response['result']: reviews = details_response['result']['reviews'] print(f"获取到{len(reviews)}条评论:") for idx, review in enumerate(reviews, 1): print(f"评论{idx}:{review['text']}") else: print("该场所暂无评论数据")
方式二:从建筑Place ID获取子场所
如果必须先定位地址对应的建筑,再获取其下的子场所,可通过两种方式实现:
方法A:利用Place Details的children字段
部分场所会返回子场所列表,对应网页版的“At this place”板块:
import requests API_KEY = "你的API密钥" building_place_id = 'ChIJ9Scljph-hYARAU-0jfBmjqY' # 调用Place Details接口,获取子场所信息 url = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={building_place_id}&key={API_KEY}&fields=children" response = requests.get(url).json() # 筛选76加油站的Place ID gas_station_id = None if 'result' in response and 'children' in response['result']: for child in response['result']['children']: if '76' in child.get('name', ''): gas_station_id = child['place_id'] print(f"找到76加油站Place ID:{gas_station_id}") break # 后续评论获取逻辑同方式一 if gas_station_id: details_url = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={gas_station_id}&key={API_KEY}&fields=reviews" # 评论处理代码...
方法B:Nearby Search搜索附近加油站
若children字段无返回,可通过建筑经纬度为中心,小半径内搜索加油站类型场所:
import requests API_KEY = "你的API密钥" building_place_id = 'ChIJ9Scljph-hYARAU-0jfBmjqY' # 先获取建筑的经纬度 geo_url = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={building_place_id}&key={API_KEY}&fields=geometry" geo_response = requests.get(geo_url).json() location = geo_response['result']['geometry']['location'] lat, lng = location['lat'], location['lng'] # 调用Nearby Search,在100米范围内搜索加油站 nearby_url = f"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat},{lng}&radius=100&type=gas_station&key={API_KEY}" nearby_response = requests.get(nearby_url).json() # 筛选76加油站 gas_station_id = None for result in nearby_response.get('results', []): if '76' in result.get('name', ''): gas_station_id = result['place_id'] print(f"找到76加油站Place ID:{gas_station_id}") break # 后续评论获取逻辑同上
注意事项
- 确保API密钥已启用Places API相关服务(Find Place、Place Details、Nearby Search)。
- 注意API配额限制,避免超出免费调用额度。
- Google Places API对英文关键词匹配精度更高,建议搜索时使用“76 gas station”。
内容的提问来源于stack exchange,提问作者Reid
相关产品推荐
相关产品推荐

