【问题标题】:PHP code to get the HTML code of a webpage [duplicate]PHP代码获取网页的HTML代码[重复]
【发布时间】:2012-02-27 10:07:06
【问题描述】:

可能重复:
How do I get the HTML code of a web page in PHP?

是否有 PHP 代码来获取网页的 HTML 代码? $html = file_get_contents('https://stackoverflow.com/questions/ask'); 这不起作用。它只是加载网页。我想在运行代码时显示 HTML 代码。

提前致谢。

【问题讨论】:

  • 然后不要在浏览器中查看输出。或者发送一个文本标题。

标签: php html


【解决方案1】:

试试:

echo htmlentities(file_get_contents('http://stackoverflow.com/questions/ask'));

【讨论】:

  • 非常感谢。此代码适用于给定的 url。但它不适用于“maduraonline.com/?find=home”。它只是显示网页。知道为什么会这样吗?当我正在开发翻译器时,我想要这个网页的 html 代码。我该怎么做才能获得它的 html 代码?非常感谢
【解决方案2】:

使用htmlentities 函数,只需对您的输出进行 HTML 编码,以便 & 变为 &。

【讨论】:

    【解决方案3】:

    或许可以试试:

    $html = htmlspecialchars(file_get_contents('http://stackoverflow.com/questions/ask'));
    

    并尝试使用标签来输出它。

    【讨论】:

      【解决方案4】:
      $handle = @fopen("'http://www.webmasterworld.com", "rt");
      $code = fread($handle,9000);
      

      这是从另一个线程复制的,但它应该足够好。它将获取 $handle 中的代码,然后 $code 将包含该代码的 9000 字节。

      【讨论】:

        【解决方案5】:

        尝试同时使用 exec 和 wget:

        <?php
        
        exec('wget http://stackoverflow.com/questions/ask -O', $array);
        
        echo implode('<br />', $array);
        
        ?>
        

        【讨论】:

          猜你喜欢
          • 2010-10-23
          • 1970-01-01
          • 2012-01-23
          • 2011-05-12
          • 1970-01-01
          • 1970-01-01
          • 2012-10-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多