【发布时间】: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