简体   繁体   中英

Extracting specific venue categories from Foursquare API using Python code

I'm trying to extract Foursquare venues data for the city of Baltimore, but only want to get specific categories (specifically things like supermarkets, grocery stores, etc.) How do I search for specific venue categories and return only venues of a specific type?

The language I'm using to extract the data that I want (from Foursquare API) is Python, and I'm working from Jupyter Notebooks. I want to limit my venue search to the following categories, however:

Farmers Market (4bf58dd8d48988d1fa941735) Grocery Store (4bf58dd8d48988d118951735) Organic Grocery (52f2ab2ebcbc57f1066b8b45) Supermarket (52f2ab2ebcbc57f1066b8b46)

I started with just one category and I don't get an error in the kernel where the code is run. When I move on to the next kernel, though, I get an error of the following type:

'TypeError: string indices must be integers'

What is this code telling me?

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)

The bit of code above runs fine in that it returns no errors. It's the code below that presents problems for me.

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

Here's the error output:

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

您可以设置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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM