python - 使用 python 的 Google Place API Json 数据
问题描述
更新:经过数小时的反复试验。
我能够将我的 google Places API json 称为返回数据并将其减少为我可以解析的列表。这很可能不是最好的方法。我知道必须直接从第一个json数据返回中获取数据??????
这是我从 google api json 数据中获取回报的代码。我采用它并减少字段并将我想要的字段纳入候选名单。)
候选清单 = [] # 清单
结果是来自 google api 调用的 json 数据。
在places_result['results']中放置:
#定义我的地点ID
my_place_id = place['place_id']
#define the fields we want sent back to us
my_fields = ['name', 'formatted_phone_number', 'type']
#make a request for the details drop some fields
place_details = gmaps.place(place_id = my_place_id, fields = my_fields)
#save results outside loop in list shortlist.append(place_details) # 是一个列表
候选名单数据示例:
入围数据:只需 2 个列表项即可查看格式:
{'html_attributions': [], 'result': {'formatted_phone_number': '(760) 864-9900', 'name': 'Palm Greens Cafe', 'types': ['cafe', 'restaurant', ' food', 'point_of_interest', 'store', '机构']}, 'status': 'OK'} {'html_attributions': [], 'result': {'formatted_phone_number': '(760) 459-4555' , '名称': '汤尼百吉饼 | 面包店 | 咖啡馆','类型':['面包店','grocery_or_supermarket','咖啡馆','餐厅','食物','point_of_interest','商店','建立']},'状态':'OK'}
我能够从候选名单中获取我在这个循环中需要的字段
####作品短结果列表i = 0 for x in shortlist: try: find_name = shortlist[i]['result']['name'] print(find_name) i = i+1
except(KeyError):
print('name error')
i= i+1
continue
#
for循环的输出:
棕榈绿咖啡镇百吉饼 | 面包店 | Cafe Starbucks Reserve McDonald's Cafe Jasmin Koffi South Palm Springs Starbucks MidMod Café Gré Coffee House & Art Gallery Vinny's Italian Ice, Frozen Custard + Ice Cream, Gelato and More L'Atelier Cafe Broken Yolk Cafe Palm Springs Ristretto Peninsula Pastries Palm Springs Camelot Internationale Cafe Rick's餐厅和面包店 星巴克 瑞士甜甜圈 星巴克 星巴克
我花了2天终于拿到这个票价。如果有更简化的方法来做这件事,只是想要建议?
在这篇文章之前,我无法通过回复获得建议的样本。
谢谢您的帮助。比尔.
解决方案
根据您使用的库,它可能已经在为您解析数据并将其放入
.json
字段中。否则,如果它不适合您,您确实需要使用json.loads
.不确定您的意思——大多数时候,您会访问数据中的特定字段,例如:
data.json['result']['name']
如果您想要字典中的所有名称:值对,您可以使用:
for name, value in data.json['result'].items()
推荐阅读
- kubernetes - Kubernetes,无法连接到服务器:EOF
- swift - UIImageView 内容不共享
- sql - 如何获取两个日期之间列中每个唯一值的计数?
- python - 从打开的 Excel 工作簿中提取数据的库
- makefile - makefile 如何为文件夹内的目标发送“是最新的”?
- java - 如何仅传递用户选择的字符串
- flutter - 在 Flutter 中的 TableRows 之间添加一些空间
- algorithm - 子集总和变化:找到总和 >= 目标的子集,超调最小
- javascript - 如何在节点,js中使用电子时读取元素的innerHTML?
- excel - Excel VBA:如何比较两个单元格的值?