Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从YelpAPI请求20多个结果?_Python_Api_Yelp - Fatal编程技术网

Python 如何从YelpAPI请求20多个结果?

Python 如何从YelpAPI请求20多个结果?,python,api,yelp,Python,Api,Yelp,我正试图从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( c

我正试图从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个以上的结果?

请尝试将排序设置为0,它最多返回1000个。否则,您应该使用
offest
参数获取数据20乘20

请尝试将排序设置为0,它最多返回1000。否则,您应该使用
offest
参数获取20乘20的数据Yelp的API最多只返回20个结果。你没有做错什么,他们只是把它限制在20

使用Google Places API获得20多个结果


然而,似乎有办法获得最多60个结果。

Yelp的API最多只返回20个结果。你没有做错什么,他们只是把它限制在20

使用Google Places API获得20多个结果


但是,似乎有一种方法可以得到最多60个结果。

您可以通过添加参数
“limit”:50来将限制增加到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

通过添加参数
“limit”:50,可以将限制增加到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

如何将其转换为python 3的urllib?我调整了
requests.get
request.request
,但随后我收到了
AttributeError:“request”对象没有属性“json”
如何将其转换为python 3的urllib?我调整了
requests.get
request.request
,但随后我收到了
AttributeError:“request”对象没有属性“json”