获取KeyError:FourSquare/Python调用中的错误

2024-10-04 05:23:02 发布

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

好吧,我是一个新手,我想我正在做我应该做的一切,但我仍然得到一个关键错误:场地。(我也试着用“场地”来代替,但我在FourSquare没有达到当天的最高限额)。。。我用一个Jupyter笔记本来做这件事

使用此代码:

版本='20200418'

半径=1000

极限=2

**url = 'https://api.foursquare.com/v2/venues/explore?client_id={}&client_secret={}&ll={},{}&v={}&radius={}&limit={}'.format(CLIENT_ID, CLIENT_SECRET, latitude, longitude, VERSION, RADIUS, LIMIT)
url
results = requests.get(url).json()**

我得到2个结果(显示在本文末尾)

当我尝试将这些结果放入数据框时,我得到“KeyError:Vinces”

# assign relevant part of JSON to venues
venues = results['response']['venues']

# tranform venues into a dataframe
dataframe = json_normalize(venues)
dataframe.head()

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-29-5acf500bf9ad> in <module>
      1 # assign relevant part of JSON to venues
----> 2 venues = results['response']['venues']
      3 
      4 # tranform venues into a dataframe
      5 dataframe = json_normalize(venues)

KeyError: 'venues'

我真的不确定我会错在哪里。。。这对我在其他地方起到了作用。。。但话说回来,就像我说的,我是新来的。。。(我还没有把我的查询排到最大,我试着用“地点”来代替)。。。多谢各位

FourSquareResults:

{'meta': {'code': 200, 'requestId': '5ec42de01a4b0a001baa10ff'},
 'response': {'suggestedFilters': {'header': 'Tap to show:',
   'filters': [{'name': 'Open now', 'key': 'openNow'}]},
  'warning': {'text': "There aren't a lot of results near you. Try something more general, reset your filters, or expand the search area."},
  'headerLocation': 'Cranford',
  'headerFullLocation': 'Cranford',
  'headerLocationGranularity': 'city',
  'totalResults': 20,
  'suggestedBounds': {'ne': {'lat': 40.67401708586377,
    'lng': -74.29300815204098},
   'sw': {'lat': 40.65601706786374, 'lng': -74.31669390523408}},
  'groups': [{'type': 'Recommended Places',
    'name': 'recommended',
    'items': [{'reasons': {'count': 0,
       'items': [{'summary': 'This spot is popular',
         'type': 'general',
         'reasonName': 'globalInteractionReason'}]},
      'venue': {'id': '4c13c8d2b7b9c928d127aa37',
       'name': 'Cranford Canoe Club',
       'location': {'address': '250 Springfield Ave',
        'crossStreet': 'Orange Avenue',
        'lat': 40.66022488705574,
        'lng': -74.3061084180977,
        'labeledLatLngs': [{'label': 'display',
          'lat': 40.66022488705574,
          'lng': -74.3061084180977},
         {'label': 'entrance', 'lat': 40.660264, 'lng': -74.306191}],
        'distance': 543,
        'postalCode': '07016',
        'cc': 'US',
        'city': 'Cranford',
        'state': 'NJ',
        'country': 'United States',
        'formattedAddress': ['250 Springfield Ave (Orange Avenue)',
         'Cranford, NJ 07016',
         'United States']},
       'categories': [{'id': '4f4528bc4b90abdf24c9de85',
         'name': 'Athletics & Sports',
         'pluralName': 'Athletics & Sports',
         'shortName': 'Athletics & Sports',
         'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/shops/sports_outdoors_',
          'suffix': '.png'},
         'primary': True}],
       'photos': {'count': 0, 'groups': []},
       'venuePage': {'id': '60380091'}},
      'referralId': 'e-0-4c13c8d2b7b9c928d127aa37-0'},
     {'reasons': {'count': 0,
       'items': [{'summary': 'This spot is popular',
         'type': 'general',
         'reasonName': 'globalInteractionReason'}]},
      'venue': {'id': '4d965995e07ea35d07e2bd02',
       'name': 'Mizu Sushi',
       'location': {'address': '103 Union Ave.',
        'lat': 40.65664427772896,
        'lng': -74.30343966195308,
        'labeledLatLngs': [{'label': 'display',
          'lat': 40.65664427772896,
          'lng': -74.30343966195308}],
        'distance': 939,
        'postalCode': '07016',
        'cc': 'US',
        'city': 'Cranford',
        'state': 'NJ',
        'country': 'United States',
        'formattedAddress': ['103 Union Ave.',
         'Cranford, NJ 07016',
         'United States']},
       'categories': [{'id': '4bf58dd8d48988d1d2941735',
         'name': 'Sushi Restaurant',
         'pluralName': 'Sushi Restaurants',
         'shortName': 'Sushi',
         'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/sushi_',
          'suffix': '.png'},
         'primary': True}],
       'photos': {'count': 0, 'groups': []}},
      'referralId': 'e-0-4d965995e07ea35d07e2bd02-1'}]}]}}

Tags: nameiddataframecountresultsunitedcategorieslng