从单击中获取API中的所有数据 - Python-Requests

时间:2014-06-04 17:49:27

标签: python api python-2.7 get python-requests

import requests
url = 'http://www.justdial.com/autosuggest.php?'
param = {
        'cases':'popular',
        'strtlmt':'24',
        'city':'Mumbai',
        'table':'b2c',
        'where':'',
        'scity':'Mumbai',
        'casename':'tmp,tmp1,24-24',
        'id':'2'
}
res = requests.get(url,params=param)
res = res.json()

虽然第一次在浏览器中点击了基本网址,但是在请求查询参数中没有显示最后3个参数,但它的工作情况。

当我点击此API时,它会返回一个包含2个键(总计和结果)的json。 结果键包含字典列表(这是主要数据)。另一个关键是' total'包含Justdial中可用的不同类别的总数。

在目前的情况下,它总共= 49,所以必须达到api 3次,因为在一次api只返回24个结果所以(24 + 24 + 1所以我们需要达到3次)。

我的问题是,是否有任何方法可以一次获得完整的json我的意思是有49个结果所以我们可以获得所有数据(所有49个类别)而不是命中api 3次。我已经在params尝试了这么多组合,但没有成功。

2 个答案:

答案 0 :(得分:0)

通常API都有countmax_results参数 - 在网址上设置此项,您将获得更多结果。

这是Twitter的API count参数的文档:https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline

答案 1 :(得分:0)

Github APi要求您检索页面中的数据(每页最多100个结果),并且响应词典的“链接”条目带有下一页结果的URL。

下面的代码遍历组织中的所有团队,直到找到正在寻找的团队

    params = {'page': 1, 'per_page':100}
    another_page = True
    api = GH_API_URL+'orgs/'+org['login']+'/teams'
    while another_page: #the list of teams is paginated
        r = requests.get(api, params=params, auth=(username, password))
        json_response = json.loads(r.text)
        for i in json_response:
            if i['name'] == team_name:
                return i['id']
        if 'next' in r.links: #check if there is another page of organisations
            api = r.links['next']['url']