带有Python的Google Place API Json数据

问题描述 投票:0回答:1

更新:经过数小时的反复试验。

我能够获得名为return data的Google Places API json,并将其简化为我可以解析的列表。这很可能不是最好的方法。我知道必须直接从第一个json数据返回中获取数据?????????

这是我的代码,可从Google api json数据中获取收益。我接受了该字段并缩小了字段,然后将我想要的字段添加到候选列表中。)

shortlist = []#列表

结果是来自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) 

#将结果保存在列表中的循环外shortlist.append(place_details)#是一个列表

入围数据样本:

短名单数据:仅2个列表项只是为了查看格式:

{'html_attributions':[],'result':{'formatted_phone_number':'(760)864-9900','name':'Palm Greens Cafe','types':['cafe','restaurant' ,“食物”,“ point_of_interest”,“商店”,“营业所”]},“状态”:“确定”}{'html_attributions':[],'结果':{'formatted_phone_number':'(760)459-4555','name':'Townie Bagels |面包店|咖啡馆”,“类型”:[“面包店”,“食品杂货店或超市”,“咖啡馆”,“餐厅”,“食物”,“兴趣点”,“商店”,“营业所”]},“状态”:“确定”}

我能够从候选清单中获取此循环中需要的字段

####简短结果的作品清单

i = 0对于x入围列表:尝试:find_name = shortlist [i] ['result'] ['name']打印(find_name)i = i + 1

except(KeyError):
    print('name error')
    i= i+1
    continue

for循环的输出:

Palm Greens CafeTownie Bagels |面包店|咖啡店星巴克储备麦当劳Cafe Jasmin科菲南棕榈泉星巴克MidMod咖啡厅格雷咖啡屋和美术馆Vinny的义大利冰块,冷冻蛋奶+冰淇淋,冰淇淋和更多L'Atelier咖啡馆棕榈茶残破的卵黄咖啡馆里斯特雷托半岛糕点棕榈泉卡米洛特国际咖啡馆里克的餐厅和面包店星巴克瑞士甜甜圈星巴克星巴克

我花了两天的时间终于得到了这个票价。如果有更简化的方法,只是想要的建议?

在发布此帖子之前,我无法从回复中获得建议的示例。

感谢您的帮助。比尔。

python json google-cloud-platform google-places-api
1个回答
1
投票
  1. 取决于您使用的是哪个库,它可能已经在为您解析数据并将其放在.json字段中。否则,如果它不适合您,则确实需要使用json.loads

  2. 不确定您的意思-大多数情况下,您将访问数据中的特定字段,例如:data.json['result']['name']

    如果要在字典中使用所有名称:值对,则可以使用:for name, value in data.json['result'].items()

© www.soinside.com 2019 - 2024. All rights reserved.