简体   繁体   中英

Four Square API: KeyError: 'venue'

I am unsure what i am doing wrong, I am trying extract foursquare Api data and have a list of restaurant by Borough,

'Neighborhood', 'ID', 'Name','ID','Rating','Tips'` 

but i keep hitting KeyError: 'venue' . I am aware KeyError is linked to daily call quota set by Four Square. i also tried checking key in dictionary , result is below. how do i retrieve the key?

    myDict = {'v': 'venue'}

print("Dictionary : ", myDict)

key = input("Please enter the Key you want to search for: ")

   # Check Whether the Given key exists in a Dictionary or Not
   if key in myDict.keys():
    print("\nKey Exists in this Dictionary")
   print("Key = ", key, " and Value = ", myDict[key])
   else:
    print("\nKey Does not Exists in this Dictionary") 

    OUTPUT
    Dictionary :  {'v': 'venue'}
   Please enter the Key you want to search for: v

 Key Exists in this Dictionary
   Key =  v  and Value =  venue

ORIGINAL ERROR

    def get_venue_details(venue_id):

CLIENT_ID = ' XXXX '# Foursquare ID, note there is a daily call quota limit 
CLIENT_SECRET =' XXXX' # Foursquare Secret, note there is a daily call quota it 
VERSION = '20180605' # Foursquare API version

#url to fetch data from foursquare api
url = 'https://api.foursquare.com/v2/venues/{}?&client_id={}&client_secret={}&v={}'.format(
        venue_id,
        CLIENT_ID, 
        CLIENT_SECRET, 
        VERSION)

# get all the data
results = requests.get(url).json()
venue_data=results['response']['venue']
venue_details=[]
try:
    venue_id=venue_data['id']
    venue_name=venue_data['name']
    venue_likes=venue_data['likes']['count']
    venue_rating=venue_data['rating']
    venue_tips=venue_data['tips']['count']
    venue_details.append([venue_id,venue_name,venue_likes,venue_rating,venue_tips])
except KeyError:
    pass

column_names=['ID','Name','Likes','Rating','Tips']
df = pd.DataFrame(venue_details,columns=column_names)
return df

   for row in indian_rest_ny.values.tolist():
  Borough,Neighborhood,ID,Name=row
try:
    venue_details=get_venue_details(ID)
    print(venue_details)
    id,name,likes,rating,tips=venue_details.values.tolist()[0]
except IndexError:
    print('No data available for id=',ID)
    # we will assign 0 value for these resturants as they may have been 
    #recently opened or details does not exist in FourSquare Database
    id,name,likes,rating,tips=[0]*5
print('(',count,'/',len(indian_rest_ny),')','processed')
indian_rest_stats_ny = indian_rest_stats_ny.append({'Borough': Borough,
                                            'Neighborhood': Neighborhood, 
                                            'ID': id,
                                            'Name' : name,
                                            'Likes' : likes,
                                            'Rating' : rating,
                                            'Tips' : tips
                                           }, ignore_index=True)

count+=1

      KeyError                                  Traceback (most recent call 
      last)
   <ipython-input-16-10411ff430c3> in <module>
     8     Borough,Neighborhood,ID,Name=row
     9     try:
---> 10         venue_details=get_venue_details(ID)
     11         print(venue_details)
     12         id,name,likes,rating,tips=venue_details.values.tolist()[0]

   <ipython-input-4-8f4e1d8ebd9f> in get_venue_details(venue_id)
 14     # get all the data
 15     results = requests.get(url).json()

---> 16 venue_data=results['response']['venue'] 17 venue_details=[] 18 try:

KeyError: 'venue'


Copy from comments:

  1. requests.get(url).json()
    resulted in NameError: name 'url' is not define.
  2. derror occurred:

     venue_data=results['response']['venue']
  3. please note this data is being extracted via Four Square API

    results = requests.get(url).json() venue_data=results["response"]['groups'][0]['items'] venue_details=[] for row in venue_data: try: venue_id = row['venue']['id'] venue_name=row['venue']['name'] venue_category=row['venue']['categories'][0]['name'] venue_details.append([venue_id,venue_name,venue_category]) except KeyError: pass column_names = ['ID','Name','Category'] df = pd.DataFrame(venue_details,columns=column_names) return df

I had the same error recently, and i found out I had used all my requests for the day on the Foursquare API.

You can get some more after registering a Credit Card on the site too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM