问题
This question is NOT about the YouTube API 500-1000 meaningful results question. The way I thought YouTube API nextPageTokens worked were that they would be null once there are no more results to return. However, I am seeing that there are results returned to begin with, but sometimes even after only 8 items (for a single request, maxResults = 50) it will include a nextPageToken but this only returns an empty 'items' list. The subsequent nextPageToken leads to more empty lists until the 1000 max is reached and then the nextPageToken becomes null.
Also concerning is that the page_info['totalResults'] shows as 1000000 but is only returning less than 200 in many cases.
So it may happen:
- Make an initial request, get 50 results
- query the nextPageToken, get another 50 results AND nextPageToken
- query the nextPageToken, get another 8 results AND nextPageToken
- query the nextPageToken, get an empty list AND nextPageToken
- query the nextPageToken, get an empty list AND nextPageToken
- etc.
Is this normal? Or do I have to look for partial 'items' lists to determine if the results are finished?
回答1:
First of all totalResults is an approximation value.
As written in docs:
The total number of results in the result set. Please note that the value is an approximation and may not represent an exact value. In addition, the maximum value is 1,000,000. You should not use this value to create pagination links. Instead, use the nextPageToken and prevPageToken property values to determine whether to show pagination links.
So it may be the count of results before any filters were applied(just speculation).
Now about nextPageToken and empty items
There are many opened issue about their weird behavior on https://issuetracker.google.com And they are working for years to fix them. see one of them
Looks like Youtube trying to optimize CPU server time for every search request with estimates or maybe the content supposed to come is restricted from your region(for example) or maybe because of some other condition met after the nextPagetokens fetched.
Is this normal? Or do I have to look for partial 'items' lists to determine if the results are finished?
IMO for now, Yes.
Possible implementations:
As long as there is a nextPageToken available, keep making successive calls. This is more recommended.
After some API calls if items become empty, then there is no need for API calls. This can be considered valid for the current condition where there are bugs in pagination implementation. But unreliable.
来源:https://stackoverflow.com/questions/51179662/youtube-api-returns-nextpagetoken-with-empty-lists