【问题标题】:Extracting geometry / location keys from Google API JSON in Python 3: AttributeError: 'list' object has no attribute 'read'在 Python 3 中从 Google API JSON 中提取几何/位置键:AttributeError: 'list' object has no attribute 'read'
【发布时间】:2023-03-16 04:20:02
【问题描述】:

我正在使用 JSON 作为输出进行地理编码,然后想要提取几何:存储纬度的位置键 |用于后续可视化的 lng 信息。我收到错误消息

AttributeError: 'list' 对象没有属性 'read'

我没有在堆栈溢出或 simplejson 网站上找到答案。我将不胜感激有关我的语法错误的指导。谢谢! PS。我删除了 google api 密钥以避免 $$。如果您愿意运行代码并以这种方式识别错误,我希望您有一个方便的方法。

导入谷歌地图

导入打印文件

将 simplejson 导入为 json

从日期时间导入日期时间

gmaps = googlemaps.Client(key='插入谷歌 API 密钥')

对地址进行地理编码

geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')

pprint.pprint(geocode_result)

python_conversion=json.load(geocode_result.read())

geo_input=json.dumps([s['geometry']['location'] for s in python_conversion['address_components']], indent=3)

pprint.pprint(geo_input)

代码的第一部分产生这个 JSON:

[{'address_components': [{'long_name': '1600', 'short_name': '1600', '类型':['street_number']}, {'long_name': '圆形剧场大道', 'short_name': '圆形剧场 Pkwy', '类型':['路线']}, {'long_name': '山景', 'short_name': '山景', '类型':['地方','政治']}, {'long_name': '圣克拉拉县', 'short_name': '圣克拉拉县', '类型':['administrative_area_level_2', '政治的']}, {'long_name': '加利福尼亚', 'short_name': 'CA', '类型':['administrative_area_level_1', '政治的']}, {'long_name': '美国', 'short_name': '美国', '类型':['国家','政治']}, {'long_name': '94043', 'short_name': '94043', '类型':['postal_code']}], 'formatted_address': '1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA', '几何':{'位置':{'lat':37.4217407,'lng':-122.0832867}, 'location_type': '屋顶', “视口”:{“东北”:{“纬度”:37.4230896802915, 'lng': -122.0819377197085}, “西南”:{“纬度”:37.4203917197085, 'lng': -122.0846356802915}}}, 'place_id': 'ChIJhehRjJ-5j4ARKFssUSrwnhY', 'plus_code': {'compound_code': 'CWC8+RP Mountain View, California, United ' '状态', 'global_code': '849VCWC8+RP'}, '类型':['street_address']}]

我提取地理坐标的第二部分是我得到所述错误的地方。

【问题讨论】:

    标签: json google-maps-api-3 python-3.6 simplejson


    【解决方案1】:

    以下是我为 Google 地方信息所做的工作:

    from googleplaces import GooglePlaces, types, lang
    YOUR_API_KEY = 'Get an API key from google'
    
    
    google_places = GooglePlaces(YOUR_API_KEY)
    
    query_result = google_places.nearby_search(
            location='Manhattan', keyword='Coffee')#,
            #radius=200000)#, types=[types.TYPE_HEALTH])
    
    if query_result.has_attributions:
        print (query_result.html_attributions)
    
    station_names=[]
    station_locs=[]
    station_ids=[]
    ct=0
    for place in query_result.places:
        # Returned places from a query are place summaries.
        station_names.append(place.name)
        ct+=1
        station_locs.append(place.geo_location)
        station_ids.append(place.place_id)
    
        # The following method has to make a further API call.
        place.get_details()
        # Referencing any of the attributes below, prior to making a call to
        # get_details() will raise a googleplaces.GooglePlacesAttributeError.
        #print (place.details) # A dict matching the JSON response from Google.
        #print (place.local_phone_number)
        #print (place.international_phone_number)
        #print (place.website)
        #print (place.url)
    
    
    #ct=0
    # Are there any additional pages of results?
    while( query_result.has_next_page_token and ct<200):
        ct+=1
        query_result_next_page = google_places.nearby_search(
                pagetoken=query_result.next_page_token)
        print("more pages")
        for place in query_result_next_page.places:
            station_names.append(place.name)
            station_locs.append(place.geo_location)
    
    station_tuples=[]
    for p in station_locs:
        station_tuples.append((float(p['lat']),float(p['lng'])))
    

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      相关资源
      最近更新 更多