电影预告片youtube search_query

时间:2017-01-25 21:47:39

标签: javascript jquery youtube-api youtube-javascript-api

任何人都可以告诉我如何获得我的第一个youtube视频链接结果?search_query = SEARCH 例: https://www.youtube.com/results?search_query=hangover+2009+trailer

第一个结果链接是: https://www.youtube.com/watch?v=tcdUhdOlz9M

任何人都可以告诉我如何用jquery做到这一点?我愿意根据search_query获得youtubes电影预告片链接。

因为此链接可以在iframe中用于显示在我的页面中。

<iframe width="560" height="315" src="https://www.youtube.com/embed/tcdUhdOlz9M" frameborder="0" allowfullscreen></iframe>

谢谢!

3 个答案:

答案 0 :(得分:1)

很简单。在将代码与Youtube API集成之后,它将返回一个类似于结果的对象,您可以使用API​​设置几乎所有内容(结果数量,日期等等)。因此,基于此,您可以像想要的那样塑造它。

下面是一个jQuery示例,其中包含一些处理结果:

function tplawesome(e,t){res=e;for(var n=0;n<t.length;n++){res=res.replace(/\{\{(.*?)\}\}/g,function(e,r){return t[n][r]})}return res}

$(function() {
    $("#search-btn").on("click", function(e){
       e.preventDefault();
        //prepare the request
        var request = gapi.client.youtube.search.list({
            part: "snippet",
            type: "video",
            q: encodeURIComponent($("#input-search").val()).replace(/%20/g, "+"),
            maxResults: 6,
            order: "viewCount",
            publishedAfter: "2015-01-01T00:00:00Z"
        });
        request.execute(function(response){
            var results = response.result;

            function getMonth(monthNumber){
                var monthName = ['jan', 'fevereiro', 'mar', 'abr', 'mai', 'jun', 'jul', 'ago', 'set', 'out', 'nov', 'dez'];

                return monthName[monthNumber-1];
            }

            var dateUTC = results.items[0].snippet.publishedAt;
            var year = dateUTC.substring(0,4);
            var day = dateUTC.substring(8,10);
            var month = dateUTC.substring(5,7);
            month = getMonth(month);

            var finalDate = day + " de " + month + " de " + year;


            console.log("passou", results.items[0].snippet.description);
            //$.each(results.items, function(index, item){
            $.get("youtube/item", function(data){ 
                $("#results").append(tplawesome(data, [{ "videoId":results.items[0].id.videoId}]));
            });
            $.get("youtube/title", function(data){ 
                $(".info-title").append(tplawesome(data,[{"title": results.items[0].snippet.title, "videoId":results.items[0].id.videoId}]));
            });
            $.get("youtube/description", function(data){ 
                $(".info-description").append(tplawesome(data,[{"description": results.items[0].snippet.description, "videoId":results.items[0].id.videoId}]));
            });
            $.get("youtube/finalDate", function(data){ 
                $(".publishedTime").append(tplawesome(data, [{finalDate}]));
            });

            $.get("youtube/relationVideo", function(data){ 
                $(".relationVideo-container").append(tplawesome(data,[{"title": results.items[1].snippet.title, "videoId":results.items[1].id.videoId}]));
            }); 
            $.get("youtube/relationVideo", function(data){ 
                $(".relationVideo-container").append(tplawesome(data,[{"title": results.items[2].snippet.title, "videoId":results.items[2].id.videoId}]));
            }); 
        });
    });
});

function init (){    gapi.client.setApiKey("AIzaSyDuLpwiCe78V9p0JE5rQaygB2XVgIDHjhs");
    gapi.client.load("youtube", "v3", function(){
       //youtube API ok 
    });
}

$( document ).ready(function(){
    $("#search-btn").on("click", function(){
       $('#info-section').addClass('show');
        $('#video-section').addClass('show');
        $('#description-section').addClass('show');
    });

您可以看到一个包含Youtube API,NodeJS和Express here的简单项目。

答案 1 :(得分:0)

您可能希望集成YouTube API:https://developers.google.com/youtube/v3/docs/search/list

但是如果你想按照上面的描述去做,你可以获得那个页面,然后抓取结果:基本上找到一些关于搜索结果的独特内容,用jquery选择器定位,然后从元素中获取你想要的数据。

答案 2 :(得分:0)

尝试以下方法:

function searchByKeyword() {
  var results = YouTube.Search.list('id,snippet', {q: 'hangover+2009+trailer', maxResults: 1});
  return results.items[0];
}