2017-01-25 2 views
-1

は、誰もが私は私のSEARCH_QUERY = SEARCH 例の最初のユーチューブビデオリンクの結果を得ることができる方法を教えてくださいすることができます https://www.youtube.com/results?search_query=hangover+2009+trailer映画の予告編ユーチューブのSEARCH_QUERY

最初の結果のリンクは次のとおりです。 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> 

ありがとうございました!

+0

サーバーを使用してください。自分でビルドする方法がわからない場合は、[diff bot](https://www.diffbot.com/dev/docs/article/) – ymz

答えて

0

おそらく、代わりにYouTubeのAPIを統合したい:https://developers.google.com/youtube/v3/docs/search/list

をしかし、あなたは、あなたが上記のように実行したい場合は、あなたがそのページを取得し、その結果をこすりできます。基本的には、検索結果についてのユニークな何かを見つけます、ターゲットをjqueryセレクタでターゲットに設定し、そのデータから必要なデータを取得します。

0

は、以下のことを試してみてください。

function searchByKeyword() { 
    var results = YouTube.Search.list('id,snippet', {q: 'hangover+2009+trailer', maxResults: 1}); 
    return results.items[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およびエクスプレスhereとの単純なプロジェクトを見ることができます。

関連する問題