我不确定自己在做什么错,我正在尝试提取foursquare Api数据并列出Borough的餐馆列表,
'Neighborhood', 'ID', 'Name','ID','Rating','Tips'`
但是我一直打KeyError: 'venue'
。我知道KeyError链接到Four Square设置的每日通话配额。我也尝试检查字典中的键,结果如下。如何获取密钥?
myDict = {'v': 'venue'}
print(“ Dictionary:”,myDict)
key = input(“请输入要搜索的密钥:”)
# 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()
---> 16个场地数据=结果['响应'] ['场地'] 17 place_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
答案 0 :(得分:0)
最近我遇到了相同的错误,发现我当天在Foursquare API上使用了所有请求。
在该网站上注册信用卡后,您还可以获得更多。