python中的Foursquare API

问题描述

我使用Foursquare API,试图制作一个数据框,其中包含多伦多每个社区中有多少个“医院”。

这是我要实现的目标:

    Neighborhood  No. of hospitals
0  Neighborhood1                 5
1  Neighborhood2                 1
2  Neighborhood3                 3
3  Neighborhood4                 4
4  Neighborhood5                 5

我的第一个选择是:

def getNearbyVenues(names,latitudes,longitudes,radius=500):

venues_list=[]
for name,lat,lng in zip(names,longitudes):
    print(name)
        
    # create the API request URL
    url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
        CLIENT_ID,CLIENT_SECRET,VERSION,lng,radius,LIMIT)
        
    # make the GET request
    results = requests.get(url).json()["response"]['groups'][0]['items']
    
    # return only relevant information for each nearby venue
    venues_list.append([(
        name,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 = ['Neighborhood','Neighborhood Latitude','Neighborhood Longitude','Venue','Venue Latitude','Venue Longitude','Venue Category']

return(nearby_venues)

Toronto_venues = getNearbyVenues(names=Toronto_df['Neighborhood'],latitudes=Toronto_df['Latitude'],longitudes=Toronto_df['Longitude']
                                  )

#next cell:

hospitals_df=Toronto_venues[(Toronto_venues['Venue Category']=='Hospital')]
hospitals_df

但是它只返回1个结果,我正在搜索的城市总共有40家医院。

我也尝试搜索吗?像这样查询

url = 'https://api.foursquare.com/v2/venues/search?client_id={}&client_secret={}&ll={},{}&v={}&query={}&radius={}&limit={}'.format(
    CLIENT_ID,latitude,longitude,search_query_hosp,LIMIT)

一个单元格:

results = requests.get(url).json()
results

一个手机:

# assign relevant part of JSON to venues
venues = results['response']['venues']

# tranform venues into a dataframe
dataframe = json_normalize(venues)
dataframe.head()

它返回了5个项目,我想解决方案是像我在探索中那样做它?查询,但是我不知道如何构造它!请帮忙:D

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...