2014-10-31 41 views
0

我想從其他網站抓取視頻到我的網站(例如,從實況視頻網站)。通過PHP從其他網站抓取iframe視頻

如何從其他網站上抓取視頻?該過程是否與刮圖像相同?

$html = file_get_contents('http://website.com/'); 
$dom = new domDocument; 
$dom->loadHTML($html); 
$dom->preserveWhiteSpace = false; 
$iframes = $dom->getElementsByTagName('frame'); 
foreach ($iframes as $iframe) { 
    $pic = $iframe->getAttribute('src'); 
    echo '<li><frame src="'.$pic.'"'; 
} 
+0

那麼什麼不適合你的代碼? – Ali 2014-10-31 14:58:55

+0

它被打印很多或錯誤,沒有視頻刮 – 2014-10-31 15:26:10

回答

0

這個職位是有點老了,不過,這是我的答案:

我建議你使用捲曲和XPath刮網站,並解析HTML數據。 file_get_content有一些安全問題,一些主機可能會禁用它。你可以做這樣的事情:

<?php 
    function scrape($URL){ 
     //cURL options 
     $options = Array(
        CURLOPT_RETURNTRANSFER => TRUE, //return html data in string instead of printing it out on screen 
        CURLOPT_FOLLOWLOCATION => TRUE, //follow header('Location: location'); 
        CURLOPT_CONNECTTIMEOUT => 60, //max time to try to connect to page 
        CURLOPT_HEADER => FALSE, //include header 
        CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Linux x86_64; rv:21.0) Gecko/20100101 Firefox/21.0", //User Agent 
        CURLOPT_URL => $URL //SET THE URL 
        ); 

     $ch = curl_init($URL);//initialize a cURL session 
     curl_setopt_array($ch, $options);//set the cURL options 
     $data = curl_exec($ch);//execute cURL (the scraping) 
     curl_close($ch);//close the cURL session 

     return $data; 
    } 

    function parse(&$data, $query, &$dom){ 
     $Xpath = new DOMXpath($dom); //new Xpath object associated to the domDocument 
     $result = $Xpath->query($query);//run the Xpath query through the HTML 
     var_dump($result); 
     return $result; 
    } 


    //new domDocument 
    $dom = new DomDocument("1.0"); 

    //Scrape and parse 
    $data = scrape('http://stream-tv-series.net/2013/02/22/new-girl-s1-e6-thanksgiving/'); //scrape the website 
    @$dom->loadHTML($data); //load the html data to the dom 

    $XpathQuery = '//iframe'; //Your Xpath query could look something like this 
    $iframes = parse($data, $XpathQuery, $dom); //parse the HTML with Xpath 

    foreach($iframes as $iframe){ 

     $src = $iframe->getAttribute('src'); //get the src attribute 
     echo '<li><iframe src="' . $src . '"></iframe></li>'; //echo the iframes 
    } 
?> 

這裏有一些鏈接,你會發現有用:

捲曲:http://php.net/manual/fr/book.curl.php

的Xpath:http://www.w3schools.com/xpath/

還有對PHP的DOM文檔機制的文檔。淨。我無法發佈鏈接,我沒有足夠的聲譽。