javascript - 无法使用页面标记 youtube api 搜索获取搜索的第二页

标签 javascript search youtube-api playlists

嗨, friend 们,我正在尝试使用 youtube api v3 搜索获取所有结果。这是代码..

 <!doctype html>
  <html>
  <head>
       <title>Search</title>
  </head>
   <body>
     <div id="buttons">
        <label> <input id="query" value='cats' type="text"/><button id="search-button"  onclick="search()">Search</button></label>
</div>
<div id="search-container">
</div>
   <button id="prev-button" class="paging-button" onclick="previousPage(document.getElementById('query').value);">Previous Page</button>
  <button id="next-button" class="paging-button" onclick="nextPage(document.getElementById('query').value);">Next Page</button><br><br>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="search.js"></script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
</body>

这是脚本代码..

var  search,nextPageToken, prevPageToken;

function search(pageToken) {
  var q = $('#query').val();
   gapi.client.load('youtube', 'v3', function() {
   gapi.client.setApiKey('Api-key');
   var request = gapi.client.youtube.search.list({
   q: q,
   part: 'snippet',
   type: 'playlist',
   maxResults: 50,
   order: 'viewCount'
    });
      if (pageToken) {
      request.pageToken = pageToken;
     }
    request.execute(function(response) {
      nextPageToken = response.result.nextPageToken;
     var nextVis = nextPageToken ? 'visible' : 'hidden';
     $('#next-button').css('visibility', nextVis);
     prevPageToken = response.result.prevPageToken
     var prevVis = prevPageToken ? 'visible' : 'hidden';
     $('#prev-button').css('visibility', prevVis);
       var playlistItems = response.result.items;
      if (playlistItems) {
        $.each(playlistItems, function(index, item) {
        displayResult(item.id);
       });
       } else {
       $('#search-container').html('Sorry you have no uploaded videos');
        }
        });
     });
     }
   function displayResult(videoSnippet) {
       var title = videoSnippet.playlistId;
       $('#search-container').append( title + '<br>');
       }


   function nextPage(val) {;
        var search_val=val;
        search(nextPageToken,search_val);
       }

   // Retrieve the previous page of videos in the playlist.
     function previousPage(listid) {
         var search=listid;
         search(prevPageToken,search);
     }

我尝试使用页面 token 获取所有结果,但无法获取所有结果我重复获得相同的 50 个结果..如何使用搜索获得准确的结果

最佳答案

要使用 pageToken 参数,您应该将在第一组结果中获得的字符串代码放置在此处。

例如,我使用Search: list使用这些参数:

- part: snippet
- maxResults: 6
- order: viewCount
- type: playlist

https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?part=snippet&maxResults=6&order=viewCount&type=playlist&_h=4&

这是结果

  • 流行音乐视频
  • 当前流行 - 俄罗斯
  • 现在很流行
  • 英国官方 40 强单曲排行榜 - 2017 年 2 月 4 日”
  • 重要视频
  • 2017 年音乐金曲 - 最佳歌曲播放列表

现在,如果我们在此请求中使用 maxResults: 3,我们将按预期获得前 3 个结果。

https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?part=snippet&maxResults=3&order=viewCount&type=playlist&_h=2&

{
 "kind": "youtube#searchListResponse",
 "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/ytSWQ-kg4VSK7qMICh0IzyEBj9k\"",
 "nextPageToken": "CAMQAA",
 "regionCode": "PH",
 "pageInfo": {
  "totalResults": 1000000,
  "resultsPerPage": 3
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/WsiG1obELyLKGKhqpeHgqP3Zr0g\"",
   "id": {
    "kind": "youtube#playlist",
    "playlistId": "PLFgquLnL59alCl_2TQvOiD5Vgm1hCaGSI"
   },
   "snippet": {
    "publishedAt": "2013-11-20T16:21:03.000Z",
    "channelId": "UC-9-kyTW8ZkZNDHQJ6FgpwQ",
    "title": "Popular Music Videos",
    "description": "",
    "thumbnails": {
     "default": {
      "url": ,
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": ",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Music",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/HSVXUxTS3sWhu_EXXqL19b-n3VE\"",
   "id": {
    "kind": "youtube#playlist",
    "playlistId": "PLgMaGEI-ZiiZ0ZvUtduoDRVXcU5ELjPcI"
   },
   "snippet": {
    "publishedAt": "2014-01-13T16:24:21.000Z",
    "channelId": "UCtxxJi5P0rk6rff3_dCfQVw",
    "title": "Popular Right Now - Russia",
    "description": "",
    "thumbnails": {
     "default": {
      "url": "",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Popular on YouTube - Russia",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/smJq1QWQX4SzucjteOJ91pLe96Y\"",
   "id": {
    "kind": "youtube#playlist",
    "playlistId": "PLrEnWoR732-BHrPp_Pm8_VleD68f9s14-"
   },
   "snippet": {
    "publishedAt": "2014-01-03T17:55:03.000Z",
    "channelId": "UCF0pVplsI8R5kcAqgtoRqoA",
    "title": "Popular Right Now",
    "description": "",
    "thumbnails": {
     "default": {
      "url": "",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Popular on YouTube",
    "liveBroadcastContent": "none"
   }
  }
 ]
}

您会在上面的结果中注意到参数“nextPageToken”:“CAMQAA”,这是您需要在下一个请求中提供以获取下一页的字符串代码,或者在此获取接下来三个结果的示例:

  • 英国官方 40 强单曲排行榜 - 2017 年 2 月 4 日”
  • 重要视频
  • 2017 年音乐金曲 - 最佳歌曲播放列表

https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?part=snippet&maxResults=3&order=viewCount&pageToken=CAMQAA&type=playlist&_h=3&

{
 "kind": "youtube#searchListResponse",
 "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/MUqIBto7KPYKqDBGqUboyp81FPs\"",
 "nextPageToken": "CAYQAA",
 "prevPageToken": "CAMQAQ",
 "regionCode": "PH",
 "pageInfo": {
  "totalResults": 1000000,
  "resultsPerPage": 3
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/yRymKW4Nn45KqMRXNQAMKnKeUB8\"",
   "id": {
    "kind": "youtube#playlist",
    "playlistId": "PL2vrmw2gup2Jre1MK2FL72rQkzbQzFnFM"
   },
   "snippet": {
    "publishedAt": "2015-01-04T15:21:08.000Z",
    "channelId": "UCchuF2Q0QhYzeUShQUm1yzQ",
    "title": "The Official UK Top 40 Singles Chart - 4th February 2017",
    "description": "The Official UK Top 40 Singles Chart [Update Every Week]",
    "thumbnails": {
     "default": {
      "url": "",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "MUSIC CHARTS",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/JCiR47eVFaF_o19Ax9V5_K2_Ux8\"",
   "id": {
    "kind": "youtube#playlist",
    "playlistId": "PL7XlqX4npddfrdpMCxBnNZXg2GFll7t5y"
   },
   "snippet": {
    "publishedAt": "2015-12-30T03:21:42.000Z",
    "channelId": "UCZj9kF7Wu2IE_-DzbQZwCEw",
    "title": "important videos",
    "description": "",
    "thumbnails": {
     "default": {
      "url": "",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "eljolto",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/BBQhDUu22HGejcoP1QvArM4001U\"",
   "id": {
    "kind": "youtube#playlist",
    "playlistId": "PLw-VjHDlEOgvtnnnqWlTqByAtC7tXBg6D"
   },
   "snippet": {
    "publishedAt": "2015-06-07T17:54:39.000Z",
    "channelId": "UCo7i93EtJhQub3SDKrtIAPA",
    "title": "Music Hits 2017 - Best Songs Playlist",
    "description": "Follow this playlist on Spotify: https:xxx/DZ8tw6 Follow this playlist on Deezer: https:xxxxJmKIaH \"Music can change the world because it can change ...",
    "thumbnails": {
     "default": {
      "url": ,
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "#RedMusic: HotMusicCharts",
    "liveBroadcastContent": "none"
   }
  }
 ]
}

然后再次使用"nextPageToken": string来获取下一页结果。

希望这些信息对您有所帮助。

关于javascript - 无法使用页面标记 youtube api 搜索获取搜索的第二页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42132945/

相关文章:

youtube - 当 Youtube 视频上传 API 时通知

javascript - $ 是在浏览器中定义的还是 JavaScript 本身定义的?

Javascript数组显示

javascript - 限制多个表上单元格的选择

javascript - Jquery 条件语句

search - Google自定义搜索引擎未提供预期的搜索结果

php - 如何在 php 中使用 Youtube Api 获取 YouTube 视频描述和评论

search - 吉拉 JQL : How to search for issue with particular component alone?

javascript - 如何在 JavaScript 中从字符串数组中搜索字符串?

YouTube:获取订阅者所在国家/地区