簡體   English   中英

如何從Yelp API請求20多個結果?

[英]How to request more than 20 results from Yelp API?

我正在嘗試從Yelp獲取多倫多餐館的完整列表。 這是我的Python代碼:

import urllib2
import rauth

params="term=restaurants&location=Toronto&limit=20&cc=CA"
consumer_key = "***"
consumer_secret = "***"
token = "***"
token_secret = "***"
session = rauth.OAuth1Session(
consumer_key = consumer_key
,consumer_secret = consumer_secret
,access_token = token
,access_token_secret = token_secret)

request = session.get("http://api.yelp.com/v2/search",params=params)


data = request.json()

問題在於Yelp只能返回20個結果。 有沒有辦法檢索20多個結果?

您可以通過添加參數'limit': 50將限制增加到'limit': 50 此外,您可以使用offset參數來遍歷記錄。 您總共可以獲取1000條記錄。 它看起來像這樣:

def get_businesses(location, term, api_key):
    headers = {'Authorization': 'Bearer %s' % api_key}
    url = 'https://api.yelp.com/v3/businesses/search'

    data = []
    for offset in range(0, 1000, 50):
        params = {
            'limit': 50, 
            'location': location.replace(' ', '+'),
            'term': term.replace(' ', '+'),
            'offset': offset
        }

        response = requests.get(url, headers=headers, params=params)
        if response.status_code == 200:
            data += response.json()['businesses']
        elif response.status_code == 400:
            print('400 Bad Request')
            break

    return data

請嘗試將sort設置為0,它最多返回1000。否則,您應使用offest參數以20乘20獲取數據

Yelp的API最多僅返回20個結果。 您沒有做錯任何事情,他們只將其限制為20。

使用Google Places API獲得20多個結果

但是,似乎有一種方法可以獲取多達60個結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM