问题描述
这个问题不是关于 YouTube API 500-1000 有意义的结果问题. 我认为 YouTube API nextPageTokens 的工作方式是,一旦没有更多结果要返回,它们就会为空.但是,我看到开始返回结果,但有时即使只有 8 个项目(对于单个请求,maxResults = 50),它也会包含一个 nextPageToken 但这仅返回一个空的项目"列表.随后的 nextPageToken 会导致更多的空列表,直到达到 1000 个最大值,然后 nextPageToken 变为空.
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.
另外令人担忧的是 page_info['totalResults'] 显示为 1000000,但在许多情况下只返回不到 200.
Also concerning is that the page_info['totalResults'] shows as 1000000 but is only returning less than 200 in many cases.
所以它可能会发生:
- 提出初始请求,获得 50 个结果
- 查询 nextPageToken,获得另外 50 个结果 AND nextPageToken
- 查询 nextPageToken,得到另外 8 个结果 AND nextPageToken
- 查询 nextPageToken,得到一个空列表 AND nextPageToken
- 查询 nextPageToken,得到一个空列表 AND nextPageToken
- 等
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?
推荐答案
首先 totalResults 是一个近似值.
First of all totalResults is an approximation value.
As written in docs:
结果集中的结果总数.请注意,该值是近似值,可能不代表精确值.此外,最大值为 1,000,000.您不应使用此值来创建分页链接.而是使用 nextPageToken 和 prevPageToken 属性值来确定是否显示分页链接.
所以它可能是应用任何过滤器之前的结果计数(只是推测).
So it may be the count of results before any filters were applied(just speculation).
现在关于 nextPageToken 和 空项目
在 https://issuetracker.google.com 上有许多关于他们奇怪行为的公开问题他们多年来一直在努力修复它们.查看其中之一
There are many opened issue about their weird behavior on https://issuetracker.google.comAnd they are working for years to fix them. see one of them
看起来 Youtube 试图通过估计来优化每个搜索请求的 CPU 服务器时间,或者可能应该来自您所在地区的内容受到限制(例如),或者可能是因为在获取 nextPagetokens 后满足了其他一些条件.
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.
这正常吗?或者我必须寻找部分项目"列表来确定结果是否完成?
IMO 目前,是的.
可能的实现:
只要有 nextPageToken 可用,就继续连续调用.更推荐这样做.
As long as there is a nextPageToken available, keep making successive calls. This is more recommended.
在一些 API 调用之后,如果项目变为空,则不需要 API 调用.对于分页实现中存在错误的当前情况,这可以被认为是有效的.但不可靠.
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.
这篇关于YouTube API 返回带有空列表的 nextPageToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!