[英]Four Square API: KeyError: 'venue'
我不確定我做錯了什么,我正在嘗試提取 Foursquare Api 數據並列出自治市鎮的餐廳列表,
'Neighborhood', 'ID', 'Name','ID','Rating','Tips'`
但我一直在打KeyError: 'venue'
。 我知道 KeyError 與 Four Square 設置的每日通話配額有關。 我也嘗試檢查字典中的鍵,結果如下。 我如何找回鑰匙?
myDict = {'v': 'venue'}
打印(“字典:”,myDict)
key = input("請輸入您要搜索的Key:")
# 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
原始錯誤
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)
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()
---> 16venue_data=results['response']['venue'] 17venue_details=[] 18 嘗試:
KeyError: '場地'
從評論復制:
requests.get(url).json()
NameError: name 'url'
未定義。發生錯誤:
venue_data=results['response']['venue']
請注意,此數據是通過 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
我最近遇到了同樣的錯誤,我發現我在 Foursquare API 上使用了當天所有的請求。
您也可以在網站上注冊信用卡后獲得更多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.