如何通过Python调用Google Maps API获取附近餐厅的网站信息?
解决方法
Google Maps Places API的places_nearby接口返回的是精简版地点数据,默认不包含网站、营业时间这类详细信息。要获取餐厅的网站,你需要用每个地点的place_id调用place_details接口来获取完整信息。
具体修改代码如下:
import googlemaps gmaps = googlemaps.Client(key='API_KEY') # 地理编码获取特拉法加广场的坐标 geocode_result = gmaps.geocode('Trafalgar Square, London, United Kingdom') location = geocode_result[0]['geometry']['location'] # 查找附近餐厅(精简数据) places_result = gmaps.places_nearby(location=location, radius=500, type='restaurant') # 遍历每个餐厅,调用place_details获取详细信息 for place in places_result['results']: place_id = place['place_id'] # 获取该地点的完整详情 details_result = gmaps.place(place_id=place_id) # 提取网站信息,处理可能不存在的情况 website = details_result['result'].get('website', '无网站信息') print(f"餐厅名称: {place['name']},网站: {website}")
注意事项
- 确保你的API密钥已开启Places API权限,否则
place_details接口会返回错误 - 部分餐厅可能未填写网站信息,用
get方法可以避免触发KeyError - API调用有配额限制,批量获取详情时注意不要超出额度
内容的提问来源于stack exchange,提问作者A Hansson
相关产品推荐
相关产品推荐

