首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过PHP从其他站点抓取iframe视频

通过PHP从其他站点抓取iframe视频
EN

Stack Overflow用户
提问于 2014-10-31 22:57:49
回答 1查看 1.9K关注 0票数 1

我想从其他网站抓取视频到我的网站(例如,从一个现场视频网站)。

如何从其他网站上抓取<iframe>视频?这个过程和抓取图片的过程一样吗?

代码语言:javascript
复制
$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.'"';
}
EN

回答 1

Stack Overflow用户

发布于 2015-05-07 05:58:14

这篇文章有点老了,但我的答案仍然是:

我建议您使用cURL和Xpath来抓取站点并解析HTML数据。file_get_content存在一些安全问题,某些主机可能会将其禁用。你可以这样做:

代码语言:javascript
复制
<?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
    }
?>

以下是一些你可能会发现有用的链接:

cURL:http://php.net/manual/fr/book.curl.php

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

在php.net上还有DomDocument文档。我不能发布链接,我没有足够的声誉。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26677609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档