實作:分頁
以下範例說明如何擷取 YouTube Data API (v3) 查詢的其他結果組合。
API 會使用 maxResults
參數,指出 API 回應應包含多少項目。幾乎所有 API 的 list
方法 (videos.list
、playlists.list
等) 都支援該參數。
如果查詢有其他結果,API 回應就會包含 nextPageToken
屬性、prevPageToken
屬性,或兩者皆有。接著,您可以使用這些屬性的值設定 pageToken
參數,藉此擷取其他結果頁面。
舉例來說,以下查詢會擷取與「skateboarding dog」查詢相符的 10 部觀看次數最多的影片搜尋結果:
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?
part=snippet
&maxResults=10
&order=viewCount
&q=skateboarding+dog
&type=video
API 回應包含查詢的前 10 個相符項目,以及可用於擷取接下來 10 個結果的 nextPageToken
屬性:
以下查詢會擷取查詢的下 10 個結果:
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?
part=snippet
&maxResults=10
&order=viewCount
&pageToken=CAoQAA
&q=skateboarding+dog
&type=video
注意:您可能需要更新 pageToken
參數的值,才能在 API Explorer 中完成這項要求。執行可擷取前 10 個結果的查詢,取得正確的 pageToken
參數值。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-11-23 (世界標準時間)。
[null,null,["上次更新時間:2024-11-23 (世界標準時間)。"],[[["The YouTube Data API (v3) uses the `maxResults` parameter to determine the number of items returned in a query response."],["API `list` methods, such as `videos.list` and `playlists.list`, support the `maxResults` parameter for pagination."],["If more results are available, the API response includes `nextPageToken` and/or `prevPageToken` properties."],["These token values can be used to set the `pageToken` parameter to retrieve additional result pages."],["The initial request fetches the first page of results, and subsequent requests use the `pageToken` from the previous response to get the next page of results."]]],["The YouTube Data API (v3) uses `maxResults` to specify the number of items in a response. `list` methods support this, and responses with additional results include `nextPageToken` or `prevPageToken`. These tokens, used with the `pageToken` parameter, retrieve further result sets. For instance, a query can get 10 most viewed videos, and a `nextPageToken` in the response allows fetching the next 10 by adding this value in the `pageToken` parameter.\n"]]