【问题标题】:Extracting specific venue categories from Foursquare API using Python code使用 Python 代码从 Foursquare API 中提取特定场地类别
【发布时间】:2019-08-06 17:49:05
【问题描述】:

我正在尝试提取巴尔的摩市的 Foursquare 场地数据,但只想获取特定类别(特别是超市、杂货店等)。如何搜索特定场地类别并仅返回特定类型?

我用来提取所需数据的语言(来自 Foursquare API)是 Python,我正在使用 Jupyter Notebooks。不过,我想将场地搜索限制在以下类别:

农贸市场 (4bf58dd8d48988d1fa941735) 杂货店 (4bf58dd8d48988d118951735) 有机杂货 (52f2ab2ebcbc57f1066b8b45) 超市(52f2ab2ebcbc57f1066b8b46)

我只从一个类别开始,并且在运行代码的内核中没有出现错误。但是,当我转到下一个内核时,我收到以下类型的错误:

'TypeError: 字符串索引必须是整数'

这段代码告诉我什么?

def getNearbyVenues(names, latitudes, longitudes, radius=2000, LIMIT=50, categoryId='52f2ab2ebcbc57f1066b8b46'):

    venues_list=[]
    for name, lat, lng in zip(names, latitudes, longitudes):
        print(name)

        # create the API request URL
        url = 'https://api.foursquare.com/v2/venues/search?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}&categoryId={}'.format(
            CLIENT_ID, 
            CLIENT_SECRET, 
            VERSION, 
            lat, 
            lng, 
            radius, 
            LIMIT,
            categoryId)

        # make the GET request
        results = requests.get(url).json()["response"]['venues'][0]['id']

        # return only relevant information for each nearby venue
        venues_list.append([(
            name, 
            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 = ['Neighborhood', 
                  'Neighborhood Latitude', 
                  'Neighborhood Longitude', 
                  'Venue', 
                  'Venue Latitude', 
                  'Venue Longitude', 
                  'Venue Category']

    return(nearby_venues)

上面的代码运行良好,没有返回错误。下面的代码给我带来了问题。

balt_markets = getNearbyVenues(names=balt_crime_df['Neighborhood'],
                                   latitudes=balt_crime_df['Latitude'],
                                   longitudes=balt_crime_df['Longitude']
                                  )

这是错误输出:

TypeError                                 Traceback (most recent call last)
<ipython-input-143-b1d736ce8ff2> in <module>
      2 balt_markets = getNearbyVenues(names=balt_crime_df['Neighborhood'],
      3                                    latitudes=balt_crime_df['Latitude'],
----> 4                                    longitudes=balt_crime_df['Longitude']
      5                                   )

<ipython-input-142-8aee194c1f65> in getNearbyVenues(names, latitudes, longitudes, radius, LIMIT, categoryId)
     27             v['venue']['location']['lat'],
     28             v['venue']['location']['lng'],
---> 29             v['venue']['categories'][0]['name']) for v in results])
     30 
     31     nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])

<ipython-input-142-8aee194c1f65> in <listcomp>(.0)
     27             v['venue']['location']['lat'],
     28             v['venue']['location']['lng'],
---> 29             v['venue']['categories'][0]['name']) for v in results])
     30 
     31     nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])

TypeError: string indices must be integers

【问题讨论】:

    标签: python jupyter-notebook foursquare


    【解决方案1】:

    您可以设置intent=browse 和逗号分隔的类别ID,例如

    https://api.foursquare.com/v2/venues/search?categoryId=50aa9e094b90af0d42d5de0d,530e33ccbcbc57f1066bbff3,530e33ccbcbc57f1066bbff9,4f2a25ac4b909258e854f55f&intent=browse&client_id={{CLIENT_ID}}&radius=30000&v=20171031&ll=51.51453,-0.12574&client_secret={{CLIENT_SECRET}}&limit=30
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多