【问题标题】:Get all data from API in single hit - Python-Requests一键从 API 获取所有数据 - Python-Requests
【发布时间】:2014-06-04 17:49:27
【问题描述】:
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 个类别),而不是 3 次点击 api。我已经在 params 中尝试了很多组合,但没有成功。

【问题讨论】:

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


    【解决方案1】:

    通常 API 有一个 countmax_results 参数 - 在 URL 上设置此参数,您将获得更多结果。

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

    【讨论】:

    • 真的吗? countmax_results 都不起作用。我将这个 url 与 count 和 max_results 一起使用,并在 chrome 插件 postman 中进行了尝试,但结果相同(我的意思是 24)justdial.com/…
    • 检查 JustDial.com API 文档,它可能被命名为其他名称,或者不存在
    【解决方案2】:

    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']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-26
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多