【问题标题】:TotalResults count doesn't match with the actual results returned in YouTube v3 search APITotalResults 计数与 YouTube v3 搜索 API 中返回的实际结果不匹配
【发布时间】:2017-09-16 08:45:41
【问题描述】:

我们正在使用 youtube v3 搜索 API。我们的“totalResults”计数和 response.items 字段中返回的项目列表不匹配。我在请求中请求 50 个视频。返回的响应显示 totalResults 计数为 65,但响应负载只有 3 个视频。

API:www.googleapis.com/youtube/v3/search/list

请求负载

{key=AIzaSyDXXQAtfbLwl6ZVoDDJPeeHKiWz3SzfOmM, maxResults=50, order=date, part=snippet, publishedAfter=2017-03-20T23:59:59.999-04:00, publishedBefore=2017-04-19T23:59:59.001-04:00, q=( ( "Wood Mackenzie" OR "Wood Mac" OR Woodmac OR @woodmackenzie ) AND NOT (fleetwood OR "fleet wood" OR 9WoodMac ) )  , type=video}

响应负载

{
 "kind": "youtube#searchListResponse",
 "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/HTW4WQODZeERIv51VrRfOhir8dg\"",
 "nextPageToken": "CDIQAA",
 "regionCode": "US",
 "pageInfo": {
  "totalResults": 65,
  "resultsPerPage": 50
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/lnNNrToZHGTehIEJ6bvEx5RZMo4\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "obzlB21t904"
   },
   "snippet": {
    "publishedAt": "2017-04-14T11:38:08.000Z",
    "channelId": "UC60Sa1LQjgy_a-K8ThN4eQw",
    "title": "Fleet wood mac Live at Leeds",
    "description": "",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/obzlB21t904/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/obzlB21t904/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/obzlB21t904/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "KEVIN MARSHALL",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/uYuuTAL3vtnFI1WOYjth0cso2Xo\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "gHAn0SbwKMg"
   },
   "snippet": {
    "publishedAt": "2017-04-06T22:17:31.000Z",
    "channelId": "UC3_MxBSSbmGznoSTgoBQ7_w",
    "title": "Beat Club: British Invasion 60's, Pt-2; Animals-Move-Fleet Wood Mac-J.Mayall-Who-Pacemakers-Kinks",
    "description": "Eric Burdon & Animals: C.C. Rider 0:01 The Move: Wild Tiger Woman 3:55 Fleet Wood Mac: Dragonfly 6:26 John Mayall: My Pretty Girl 9:47 Thunderclap ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/gHAn0SbwKMg/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/gHAn0SbwKMg/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/gHAn0SbwKMg/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "ROCK&POPS1_koba",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/tapelxOG4Q3gZRJCR2qaVdt-ZO8\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "5GvLbFBdcF4"
   },
   "snippet": {
    "publishedAt": "2017-04-02T04:48:47.000Z",
    "channelId": "UC6fn383TI1HVDYW4dSEIogQ",
    "title": "\"Dreams\" by fleet wood mac (vocal cover)",
    "description": "I do not own this song. I do not make a profit off of this song. It belongs to the person(s) who wrote it. Please subscribe and comment below any suggestions?!",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/5GvLbFBdcF4/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/5GvLbFBdcF4/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/5GvLbFBdcF4/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Musikallitee",
    "liveBroadcastContent": "none"
   }
  }
 ]
}

【问题讨论】:

    标签: youtube youtube-api youtube-data-api


    【解决方案1】:

    documentation 本身所述,totalResults 是:

    整数

    结果集中的结果总数。请注意,该值是近似值,可能不代表精确值。另外,最大值为1,000,000。

    您不应使用此值来创建分页链接。而是使用 nextPageTokenprevPageToken 属性值来确定是否显示分页链接。

    该值只是一个近似值/估计值,而不是返回的items 总数的确切值。我已经使用与您相同的详细信息测试了 API,其值往往会从 63 变为 6465

    综上所述,您的代码没有任何问题。这只是预期的行为。不幸的是,在任何地方都没有说明totalResults 的值的依据或最佳使用位置。最好继续执行不依赖此参数的实现。

    【讨论】:

    • 感谢您的回复,非常感谢。
    猜你喜欢
    • 2014-07-09
    • 2013-06-25
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多