我收到此密钥错误:尝试使用Foursquare API获取附近场地时'groups'



嗨,我在尝试使用 Foursquare API 获取附近的场地时遇到keyerror: 'groups'。以下是我的代码:

LIMIT = 100 # limit of number of venues returned by Foursquare API

radius = 500 # define radius
venues_list = []
for lat, long, post, borough, neighborhood, hospital in zip(hospital_df['Latitude'], hospital_df['Longitude'], hospital_df['Pincode'], hospital_df['District'], hospital_df['Location'], hospital_df['Hospital_Name']):
print(neighborhood)
print(borough)
url = "https://api.foursquare.com/v2/venues/explore?client_id={}&client_secret={}&v= 
{}&ll={},{}&radius={}&limit={}".format(
CLIENT_ID,
CLIENT_SECRET,
VERSION,
lat,
long,
radius, 
LIMIT)
results = requests.get(url).json()["response"]['groups'][0]['items']
venues_list.append([(
post, 
borough,
neighborhood,
hospital,
lat, 
lng, 
v['venue']['name'], 
v['venue']['location']['lat'], 
v['venue']['location']['lng'],  
v['venue']['categories'][0]['name']) for v in results])
nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])
nearby_venues.columns = ['PostalCode', 'Borough', 'Neighborhood', 'Hospital', 'Neighborhood_Latitude', 'Neighborhood_Longitude', 'VenueName', 'VenueLatitude', 'VenueLongitude', 'VenueCategory']

我不断收到以下错误:

键错误:"组">

我遇到了同样的问题"KeyError:'groups'",代码非常相似。我发现,虽然我构建的 URL 变量格式不正确(我为 categoryId 添加了一个过滤器,但我为此值传递了一个格式不正确的变量(。

一旦我更正了这个公式,"results = requests.get(url(.json((["response"]['groups'][0['items']"能够处理而不会出错。

我的猜测是,当我提交格式不正确的 URL 时,JSON 返回是一条错误消息,没有包含"组"的正确格式。

这是我的情况中,这是由于超过了对Foursquare API的免费调用次数。我有非常相似的代码,作为在线课程的一部分预先编写。我运行了很多天,没有任何问题。然后,突然间我得到了几次"组"键错误。然后,我停止工作,第二天早上代码运行良好。然后,在几个电话之后,我再次收到错误。所以我检查了.json文件,它不包含关键的"组",因为它基本上是一个.json文件,告诉我超过了配额。

尝试从Foursquare帐户重置客户端密钥。它对我有用。

最新更新