【问题标题】:How can I get Youtube comments using Jquery and Youtube api V3?如何使用 Jquery 和 Youtube api V3 获取 Youtube 评论?
【发布时间】:2016-09-07 07:02:35
【问题描述】:

所以,我在这个主题中看到了很多问题,但所有答案都是从旧的 gdata url 开始的,这不再起作用了。
如果我单击此页面上的(附加)按钮,我想为每个视频获取 cmets:
https://www.youtube.com/feed/subscriptions
显然这是一个篡改脚本。
我的结局是,我想从内容中获得一点点偷窥,所以我可以决定是否点击 youtube 链接。
也许我计划为 cmets 提供回复功能,但这只是一个未来的计划。
我现在拥有的:

// ==UserScript==
// @name         hovercards for youtube
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.youtube.com/*
// @grant        none
// @require     https://code.jquery.com/jquery-latest.min.js

// ==/UserScript==
/* jshint -W097 */
  //'use strict';

jQuery(document).ready(function() {
$("a.yt-uix-tile-link,.yt-ui-ellipsis-2k").on("mouseover",function() {
         //Replace video block start    
$(function() {
    $('a.yt-uix-tile-link,.yt-ui-ellipsis-2k').each(function() {
        var yt_url = this.href,
            yt_id = yt_url.split('?v=')[1],
            yt_title = $(this).text();
        $(this).replaceWith('<div class="youtube-box" style="background-image:url(https://img.youtube.com/vi/' + yt_id + '/0.jpg);"><span class="youtube-title">' + yt_title + '</span><span class="youtube-bar"><span class="yt-bar-left"></span><span class="yt-bar-right"></span></span> </div>');
        $("div.youtube-box").on("mouseover",function() {
            $(this).replaceWith('<iframe width="560" height="315" frameborder="0" allowfullscreen class="youtube-frame" src="https://www.youtube.com/embed/' + yt_id + '?autoplay=1"></iframe>');
        });
    });
});
  //Replace video block end
});

});

我想在这个时候将代码分开工作,所以我当然想在工作格式上添加这个:

jQuery(document).ready(function() {
jQuery("'a.yt-uix-tile-link,.yt-ui-ellipsis-2k").append("<input type='button' value='Give me my comments' class='button'>");
jQuery(".button").on("click",function() {
jQuery.getJSON('https://www.googleapis.com/youtube/v3/comments?id=yVqreR8VXwQ&key=YOURAPIKEY&part=snippet',function(data){
//Replace the YOURAPI key section for your key
 if (typeof(data.items[0]) != "undefined") {
     console.log('video exists ' + data.items[0].snippet.comment);
     $(".result").append( data.items[0].snippet.comment);
   } else {
     console.log('video not exists');
     jQuery("result").append("Nope, we don't get any data");
 }   
});
});
});

当我用我的 api 密钥查看浏览器时,这个 URL:
'https://www.googleapis.com/youtube/v3/comments?id=yVqreR8VXwQ&amp;key=YOURAPIKEY&amp;part=snippet'
我得到了这个结果:

{
 "kind": "youtube#commentListResponse",
 "etag": "\"kiOs9cZLH2FUp6r6KJ8eyq_LIOk/pGLBhpjR05yQoJV31WoAx2PEFVw\"",
 "items": []
}

我不明白为什么,为什么我没有收到任何物品?
该网址对我来说看起来不错。

【问题讨论】:

    标签: jquery youtube tampermonkey youtube-data-api


    【解决方案1】:

    可能会这样

    GET https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId={YOUR_Video_ID}&key={YOUR_API_KEY}
    

    您可以在此处查看所需信息。

    https://developers.google.com/youtube/v3/docs/commentThreads#methods

    【讨论】:

      【解决方案2】:
       获取 https://www.googleapis.com/youtube/v3/commentThreads?key=AIzaSyDDBk8tAkod1VRRNyFZF09fgQyMpnSe5HI&textFormat=plainText&part=sn-p&videoId=kffacxfA7G4&maxResults=50 

      你可以试试这个,对我来说效果很好。

      【讨论】:

      • 嗯,根据你的回答看起来不错我做了这个:jsfiddle.net/bTLX8/15 唯一的问题是我们不仅有一个视频,而且还有很多很多,我们需要将视频 ID 字符串更改为实际的视频 ID .这怎么可能?
      猜你喜欢
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 2018-03-19
      • 2012-01-31
      • 2015-08-23
      相关资源
      最近更新 更多