Google Maps Nearby Search API 拉取循环返回不需要的元素

Google Maps Nearby Search API pulling loop returns unwanted elements

提问人:Rhinosaur 提问时间:9/23/2023 最后编辑:UliRhinosaur 更新时间:9/23/2023 访问量:14

问:

我对 python 非常陌生,我正在尝试制作一个循环来提取 Google 附近的搜索数据(我知道,对于菜鸟来说,这是一个非常雄心勃勃的项目)。我根据我的理解和我找到的几个不同的 Google API 教程编写了一个代码循环。这是我正在拉取的 API

Bangunkerto = gmaps.places_nearby(location = '-7.644826,110.3576912',  radius = 5000, keyword = 'warmindo')
pp.pp(Bangunkerto)

API 显示了一个字典,其中包含多个不同“结果”键的列表,其中还有其他几个键,例如“business_status”、“名称”等。

def warmindo_name(Bangunkerto):
    Bangunkerto_data = []
    for i in range(len(Bangunkerto['results'])):
        data = dict(Status = Bangunkerto['results'][i]['business_status'],
                    Name = Bangunkerto['results'][i]['name'],
                    Rating = Bangunkerto['results'][i]['rating'],
                    RatingCount = Bangunkerto['results'][i]['user_ratings_total'],
                    Lat = Bangunkerto['results'][i]['geometry']['location']['lat'],
                    Lng = Bangunkerto['results'][i]['geometry']['location']['lng'],
                    Address = Bangunkerto['results'][i]['vicinity'])
        Bangunkerto_data.append(Bangunkerto)
    return(Bangunkerto_data)

当我尝试在自己上运行变量并且它工作正常时,它会返回我想要的唯一键。data

{'Status': 'OPERATIONAL', 'Name': 'Warung Makan Indomie (Warmindo)', 'Rating': 0, 'RatingCount': 0, 'Lat': -7.6254195, 'Lng': 110.314549, 'Address': 'Semen, Sucen, Magelang Regency'}

但是当我将代码作为循环运行时,会出现问题。它返回我不想要的其他键,例如“place_id”、“rating”、“scope”等。

[{'html_attributions': [],
  'results': [{'business_status': 'OPERATIONAL',
    'geometry': {'location': {'lat': -7.664047900000001, 'lng': 110.3933883},
     'viewport': {'northeast': {'lat': -7.662670570107276,
       'lng': 110.3946672798927},
      'southwest': {'lat': -7.66537022989272, 'lng': 110.3919676201073}}},
    'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/restaurant-71.png',
    'icon_background_color': '#FF9E67',
    'icon_mask_base_uri': 'https://maps.gstatic.com/mapfiles/place_api/icons/v2/restaurant_pinlet',
    'name': 'Warmindo "Kang Heri"',
    'opening_hours': {'open_now': False},
    'photos': [{'height': 1080,
      'html_attributions': ['<a href="https://maps.google.com/maps/contrib/118356888274530418424">Aswanudin Hamid</a>'],
      'photo_reference': 'ATJ83zhCJvZksjzhQUvftnGFH7ejpoIOSsd3wexV7ulAKjFp68l34OfIT56I0sE70fnP610sHuPALFZLnM7Wfh-7Q-uCbF2H2zczRf3chPririZRUe4j_9_pxqz7y8VN0Jrya1vfIPGLHMgjIvMIgQzuOAXsdM15BfpSLsFrwCNoOplBEual',
      'width': 1920}],
    'place_id': 'ChIJs5qxkrJfei4RhNTu6cqojDA',
    'plus_code': {'compound_code': '89PV+98 Purwobinangun, Sleman Regency, Special Region of Yogyakarta',
     'global_code': '6P4G89PV+98'},
    'rating': 5,
    'reference': 'ChIJs5qxkrJfei4RhNTu6cqojDA',
    'scope': 'GOOGLE',
    'types': ['restaurant', 'food', 'point_of_interest', 'establishment'],
    'user_ratings_total': 8,
    'vicinity': 'Jl. Jogja - Turi, Glondong, Purwobinangun, Kabupaten Sleman'},
   {'business_status': 'OPERATIONAL',
    'geometry': {'location': {'lat': -7.6254195, 'lng': 110.314549},
     'viewport': {'northeast': {'lat': -7.624024570107278,
       'lng': 110.3159549798927},
      'southwest': {'lat': -7.626724229892722, 'lng': 110.3132553201073}}},
    'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/restaurant-71.png',
    'icon_background_color': '#FF9E67',
    'icon_mask_base_uri': 'https://maps.gstatic.com/mapfiles/place_api/icons/v2/restaurant_pinlet',
    'name': 'Warung Makan Indomie (Warmindo)',
    'photos': [{'height': 1728,
      'html_attributions': ['<a href="https://maps.google.com/maps/contrib/106834421251748392066">Christian Bannard</a>'],
      'photo_reference': 'ATJ83zh9127TB73znUnyrPy_Yyzz1RYT3IvSNnyBRyaqvXsRqEMea6HJDok_bIycRGT0PVEq38wY7-eSXZqR9CBNgTRmpgXqsplwFz8sOFh2ZOQaCl28IDpDGQkcdhU7L36LQeLDTIIiC-VgajiTLz2AbZm4F0-Vhv76hrYRclq1OhVMtLdF',
      'width': 3840}],
    'place_id': 'ChIJi0QW6rCKei4RLWANXIlD9d0',
    'plus_code': {'compound_code': '98F7+RR Sucen, Magelang Regency, Central Java',
     'global_code': '6P4G98F7+RR'},
    'rating': 0,
    'reference': 'ChIJi0QW6rCKei4RLWANXIlD9d0',
    'scope': 'GOOGLE',
    'types': ['restaurant', 'food', 'point_of_interest', 'establishment'],
    'user_ratings_total': 0,
    'vicinity': 'Semen, Sucen, Magelang Regency'},

我的问题是,如何从每个“结果”中只返回我想要的键?任何帮助将不胜感激。提前致谢!

python 函数 循环 google-api

评论


答: 暂无答案