2016-03-01 35 views
-3

我想寫一個正則表達式爲jquery從youtube搜索結果中獲取所有數據。這是一個Chrome擴展,所以當它被集成到鉻瀏覽器,當你打開youtube像 https://www.youtube.com/results?search_query=american+idol 我想獲取當前視頻的結果頁面中的所有數據。當你檢查元素。例如 -正則表達式爲jquery檢查模式並提取HTML

<h3 class="yt-lockup-title "> 
    <a class="yt-uix-sessionlink yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 spf-link " dir="ltr" rel="spf-prefetch" aria-describedby="description-id-565489" title="Tristan McIntosh - Top 10 - AMERICAN IDOL" data-sessionlink="itct=CDoQ3DAYASITCPjJ3uXfn8sCFVQOaAodar4E7yj0JFINYW1lcmljYW4gaWRvbA" href="/watch?v=XfSwQr85dzw">Tristan McIntosh - Top 10 - AMERICAN IDOL</a> 

我想(在HREF v=後,即ID)來從這裏title和視頻ID從href

然後從這個HTML我想data-ytid

<div class="yt-lockup-byline"> 
    <a class="yt-uix-sessionlink g-hovercard spf-link " data-ytid="UCAMPco9PqjBbI_MLsDOO4Jw" data-sessionlink="itct=CDoQ3DAYASITCPjJ3uXfn8sCFVQOaAodar4E7yj0JA" href="/user/americanidol">American Idol</a> 

從這個HTML我想列出。

<div class="yt-lockup-content"> 
<h3 class="yt-lockup-title "> 
<div class="yt-lockup-byline"> 
<div class="yt-lockup-meta"> 
<ul class="yt-lockup-meta-info"> 
<li>9 hours ago</li> 
<li>1,792 views</li> 

基本上我希望所有結果的youtube stat結果的所有統計數據。像data-ytidtitleviewsvideo countdescription

我不知道如何寫這個正則表達式。在jQuery中。

我試着寫PHP中的正則表達式爲其中之一 -

preg_match_all('%<h3[^>]+yt-lockup-title[^>]+><a[^>]+href\=\"([^"]+)\"[^>]+>%isu', $html, $links); 

但我必須寫在jQuery的東西,對結果頁上的所有視頻獲取所有這些值。任何人都可以請幫忙。

我希望我的問題很清楚。

感謝

+0

首先,[不使用正則表達式對本](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags),解析HTML並遍歷它。其次,更重要的是,我不認爲這甚至是可能的,因爲您可能會被阻止檢索相同來源策略的YT頁面的HTML –

+0

@Rory McCrossan感謝您的鏈接幫助理解了一些事情,並且我確實解決了這個問題。 :) – Akash

回答

0

這是我如何解決它,如果它派上用場了別人以後。

//Function to fetch the YouTube Id, Video Id, Title and Views 
function getDom(className) { 
    if(className == '.yt-lockup-byline' || className == '.yt-lockup-title') { 
     var items = $(className).find('a'); 
    } else if(className == '.yt-lockup-meta-info') { 
     var items = $(className).find('li'); 
    } 
    var length = items.length; 
    var position = -1; 
    var obj_b = next(); 

    function next() { 
     position = (position + 1 < length) ? position + 1 : 0; 
     return items.eq(position); 
    } 

    if (className == '.yt-lockup-byline') { 
     $.each(obj_b.prevObject, function() { 
      var str_result = this.attributes[2].nodeValue; 
      console.log("YouTube ID :- "+str_result); 
     }); 
    } else if (className == '.yt-lockup-meta-info') { 
     $.each(obj_b.prevObject, function() { 
      var str_result = this.innerHTML; 
      console.log("Views :- "+str_result); 
     }); 
    } else if (className == '.yt-lockup-title') { 
     $.each(obj_b.prevObject, function() { 
      var str_result = this.attributes[3].nodeValue; 
      var str_video_id = this.attributes[0].nodeValue; 
      console.log("YouTube Title :- "+str_result); 
      console.log("YouTube Video ID :- "+str_video_id); 
     }); 
    } 

} 
//For fetching YotuTube Id 
getDom('.yt-lockup-byline'); 
//For fetching views and release date 
getDom('.yt-lockup-meta-info'); 
//For fetching title and video id 
getDom('.yt-lockup-title');