我使用了带有categoryId的Foursquare Explore API查询特定半径内每个类别的场所数。响应包含指定坐标,半径和类别的totalResults值。
def get_venues_count(latitudes, longitudes, radius, categoryId):
explore_url = 'https://api.foursquare.com/v2/venues/explore?client_id={}&client_secret={}&v={}&ll={},{}&radius={}&categoryId={}'.format(
CLIENT_ID,
CLIENT_SECRET,
VERSION,
lat,
lng,
radius,
categoryId)
#make the GET request
return requests.get(explore_url).json()['response']['totalResults']
#Create new dataframe to store venues data
stations_venues_df = df.copy()
for c in categories_list:
stations_venues_df[c[0]] = 0
#Request number of venues, store result as CSV
for i, row in stations_venues_df.iterrows():
print(i)
for c in categories_list:
stations_venues_df.loc[i, c[0]] = get_venues_count(stations_venues_df.Latitude.iloc[i],
stations_venues_df.Longitude.iloc[i],
radius=500,
categoryId=c[1])
stations_venues_df.to_csv('stations_venues.csv')
我运行了很多次,但是每次它只生成几行然后停下来并显示此错误:
KeyError Traceback (most recent call last)
<ipython-input-22-551e888d2436> in <module>
6 stations_venues_df.Longitude.iloc[i],
7 radius=500,
----> 8 categoryId=c[1])
9 stations_venues_df.to_csv('stations_venues.csv')
<ipython-input-20-c9e33ae6e146> in get_venues_count(latitudes, longitudes, radius, categoryId)
10
11 # make the GET request
---> 12 return requests.get(explore_url).json()['response']['totalResults']
KeyError: 'totalResults'
答案 0 :(得分:0)
我很可能会猜测返回的响应无效(或者您期望使用其他json格式)。
我建议添加一个检查“ totalResults”是否在json()[“ reponse”]字典中。 如果没有,那么您将知道为什么以及返回哪种响应。