【问题标题】:What would be a more efficient method to paginate through this API?通过此 API 进行分页的更有效方法是什么?
【发布时间】:2022-02-18 21:20:54
【问题描述】:

我创建了几行代码来通过 API 进行分页,但时间复杂度为 O(n^2),我很好奇是否有更有效的方法来实现此代码。

#Example JSON Response
[
  {
    "internalName": "DEUSEXHUMANREVOLUTIONDIRECTORSCUT",
    "title": "Deus Ex: Human Revolution - Director's Cut",
    "metacriticLink": "/game/pc/deus-ex-human-revolution---directors-cut",
    "dealID": "HhzMJAgQYGZ%2B%2BFPpBG%2BRFcuUQZJO3KXvlnyYYGwGUfU%3D",
    "storeID": "1",
    "gameID": "102249",
    "salePrice": "2.99",
    "normalPrice": "19.99",
    "isOnSale": "1",
    "savings": "85.042521",
    "metacriticScore": "91",
    "steamRatingText": "Very Positive",
    "steamRatingPercent": "92",
    "steamRatingCount": "17993",
    "steamAppID": "238010",
    "releaseDate": 1382400000,
    "lastChange": 1621536418,
    "dealRating": "9.6",
    "thumb": "https://cdn.cloudflare.steamstatic.com/steam/apps/238010/capsule_sm_120.jpg?t=1619788192"
  },
response = requests.get("https://www.cheapshark.com/api/1.0/deals")
numPages = response.headers['X-Total-Page-Count']

entries = []

for page in range(0, int(numPages):
    url = f"https://www.cheapshark.com/api/1.0/deals&pageNumber={page}"
    data = requests.get(url).json()
    for i in range(60):
        allData = {
            'title' : data[i]['title'],
            'salePrice' : data[i]['salePrice']
        }
        entries.append(allData)
    page += 1

【问题讨论】:

  • 如果不关心顺序,可以并行调用

标签: python json pagination time-complexity api-design


【解决方案1】:

如果那是服务提供的 API,那么不——你会得到尽可能高效的。

顺便说一句:我看到你在做for i in range(60) 并猜测每页有 60 个项目?最好简单地遍历data 中的所有条目,以防每页的项目数随时间变化(例如,想象最后一页——您可能没有完整的 60 个项目...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    相关资源
    最近更新 更多