简体   繁体   中英

Foursquare Venue API & Number of Results, in a more efficient way?

I'd like to ask if there is a more efficient way to get more than 50 results besides these options?

I'm using the current foursquare api for the venue search https://developer.foursquare.com/docs/venues/search .

What I'd like is something like an offset option, in order to get more results, but it seems that there isn't such an option.

Is there an alternative solution? Thank you in advance.

you should use venues explore with offset and limit as paramters, venues explore gives you totalResults and you can use this response to calculate number of pages you need in paginate

for example assume totalResults is 90(pay attention at offset and limit parametr value ) in first request: https://api.foursquare.com/v2/venues/explore?client_id=client_id&client_secret=client_secret&v=20150825&near=city_name&categoryId=category_id&intent=browse&offset=0&limit=30

in second request: https://api.foursquare.com/v2/venues/explore?client_id=client_id&client_secret=client_secret&v=20150825&near=city_name&categoryId=category_id&intent=browse&offset=30&limit=30

in third request: https://api.foursquare.com/v2/venues/explore?client_id=client_id&client_secret=client_secret&v=20150825&near=city_name&categoryId=category_id&intent=browse&offset=60&limit=30

for 90 results you can get all records with above three request

There is actually another option not mentioned here (not pagination though)

Using the (experimental?) categoryId filter.

You can search for a single point (ll) a few times with different category ids, giving you many results (some duplicates as venues can have more than one category).

So you can search for 'Food' venues and 'Nightlife' venues at the same place, getting 100 results in stand of 50.. as said it is 100 results, but not unique results, could be duplicates. I think that is more efficient then trying to play around with the browse radius thing.

Not pagination, but will give a lot more results than a normal search - usually enough even in urban areas.

But yea, having some sort of way to extract more than 50 on a single point is not possible, but could be nice :)

For the explorer endpoint this worked for me: If the maximum number of results that is returned for instance is 100, just use offset=100 in the next call which gives you the next 100 results starting from 100 (the offset). Iterate (eg using while loop) and keep increasing offset by 100 until you reach the total number or results (which is returned in in the api for totalResults). My first stack overflow post, tried to answer as clearly as possible

def getNearbyVenues(neighborhoods, latitudes, longitudes, radius=500,ven_num=300):
 
    venues_list=[]
    for name, lat, lng in zip(neighborhoods, latitudes, longitudes):
        i=0
        while (i < ven_num+50):
            
        
            url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&offset={}&limit={}'.format(
            CLIENT_ID, 
            CLIENT_SECRET, 
            VERSION, 
            lat, 
            lng, 
            radius,
            i,
            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, 
            lat, 
            lng, 
            v['venue']['name'], 
            v['venue']['location']['lat'], 
            v['venue']['location']['lng'],  
            v['venue']['categories'][0]['name']) for v in results])
            i=i+50
            

    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']
    print('Ok')
    return(nearby_venues)

the code above has worked perfectly with me, where ven_num variable is the desired limit for calling venues in a certain neighborhood

Afraid not. Currently there is no pagination, in order to find more venues you need to move your search area around as in the answers you highlighted. I agree, pagination would be handy though!

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