【问题标题】:Can't load youtube source in php script.无法在 php 脚本中加载 youtube 源。
【发布时间】:2012-12-27 14:48:54
【问题描述】:

您好,我正在制作一个 php 脚本来从 Youtube 结果中提取视频 URL。我有这个:

<?php
    error_reporting(1);

    function conseguir_codigo_url($url) {
        $dwnld = curl_init();
        curl_setopt($dwnld, CURLOPT_URL, $url);
        curl_setopt($dwnld, CURLOPT_HEADER, 0);
        //$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.01; Windows NT 6.0)';
        curl_setopt($dwnld, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($dwnld, CURLOPT_RETURNTRANSFER, true);

        $fuente_url = curl_exec($dwnld);
        curl_close($dwnld);
        return $fuente_url;
    }

    function extraer_atributo_elemento($fuente) {
        $file = new DOMDocument;

        if($file->loadHTML($fuente) and $file->validate()){

            echo "DOCUMENTO";

            $file->getElementById("search-results");

        }

     $codigo_url = conseguir_codigo_url("http://www.youtube.com/results?search_sort=video_date_uploaded&uni=3&search_type=videos&search_query=humor");
    extraer_atributo_elemento($codigo_url);
?>

问题是我不能使用getelementbyid,我想可能是html5。你有解决这个问题的建议吗?我需要解析源代码,但我不知道 regex 。所以 domdocument 是唯一的方法。

【问题讨论】:

  • 首先你应该考虑使用 SimpleXML php.net/manual/de/book.simplexml.php
  • 这不太可能带您到任何地方。 YouTube 竭尽全力使他们的内容无法下载。无论哪种方式,您都应该提供更具体的问题描述。究竟出了什么问题?
  • 其次,您应该真正使用英语方法。这可能有助于更快地理解代码!

标签: php html youtube domdocument


【解决方案1】:

你为什么使用$file-&gt;validate()?如果你只是想通过 ID 提取元素,不需要调用它。此外,在调用 loadHTML 之前将 DOMDocument::recover 设置为 true 可能有助于解析来自网络的损坏的 HTML。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-07
    • 2016-05-06
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2021-06-18
    相关资源
    最近更新 更多