如何解析google结果,以可读的格式获取名称、纬度和经度?

2024-05-31 23:30:56 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮帮我

我正在运行一个查询来查找特定位置半径内的所有条。代码工作产生这个结果。 然而,这一结果很难从实际中获得任何信息。 我需要的几何(纬度和长)为每个结果,最好的名称了。我希望这些信息自动填充到一个新的数据框中,但即使现在打印它也会很完美。你知道吗

import googlemaps
from datetime import datetime
from googlemaps import convert

gmaps = googlemaps.Client(key='my_api_key_is_here')

query = 'bar'
location = (54.584520, -5.935232)
radius = 500

local = gmaps.places('bar', location = location, radius = radius)

#This is what I attempted
#print local['local']['results'][0]['titleNoFormatting']

print local
.... {u'results': {u'geometry': {u'location' {u'lat' 54.343434, u'lng': -5.23423423}}....} }

当我打印它时,它看起来像下图。如何访问每个酒吧的特定lat和long?你知道吗

如何访问以下内容:

{u'geometry': {u'location' {u'lat' 54.343434, u'lng': -5.23423423}}....}

我对python非常陌生,所以非常感谢任何非常明显的帮助!你知道吗


Tags: keyfromimport信息datetimeislocalbar
2条回答

您需要对结果进行迭代。你知道吗

您可以通过执行以下操作来访问几何体值:bar[“geometry”]等等。你知道吗

local = gmaps.places('bar', location=location, radius=radius)
for bar in local['results']:
    loc = bar['geometry']['location']
    print(
        '{} ({}, {})'
        .format(bar['name'], loc['lon'], loc['lat'])
    )

您可以在这里阅读有关python词典的更多信息:https://docs.python.org/2/tutorial/datastructures.html#dictionaries

似乎您得到的是一个JSON对象。它是被结构化为文本的数据;这意味着它是一个字符串,直到您实际将其解析为Python对象为止。你知道吗

要将其转换为Python字典,请尝试以下操作:

import json

my_data = json.loads(local)
print(my_data['results'])

相关问题 更多 >