doufuhuang6981
2016-11-04 15:33 阅读 71
已采纳

如何用PHP语言从HTML文件创建DOM对象?

I want to parsing data from homepage on this url. As you can see this url is HTML file and I read below:

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');

so I just type a code below

include "simple_html_dom.php";
$html = file_get_html('eecs.kookmin.ac.kr/site/computer/notice.htm');
echo $html->plaintext;

The error message is:

Error message Warning: file_get_contents(eecs.kookmin.ac.kr/site/computer/notice.ht‌​m): failed to open stream: No such file or directory in C:\Bitnami\wampstack-5.6.27-0\apache2\htdocs\simple_html_dom‌​.php on line 76

what should I do?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doupuxuan5784 doupuxuan5784 2016-11-04 16:49

    You can get the HTML code using the Snoopy Class (https://sourceforge.net/projects/snoopy). Next code displays the HTML code inside of a <textarea> tag, then it displays the page itself, copy-paste next code in a PHP file and open it in your browser:

    <!DOCTYPE html>
    <html>
      <head>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=euc-kr">
        <META HTTP-EQUIV="Content-language" CONTENT="ko">
      </head>
      <body>
    <?php
    require("Snoopy.class.php"); // ◄■■ GET SNOOPY FROM https://sourceforge.net/projects/snoopy
    $snoopy = new Snoopy;
    $snoopy->fetch("http://eecs.kookmin.ac.kr/site/computer/notice.htm");
    $html = mb_convert_encoding( $snoopy->results, "UTF-8", "EUC-KR" ); // ◄■■ GET HTML CODE.
    echo "<textarea rows='25' cols='80'>$html</textarea>"; // ◄■■ DISPLAY THE HTML.
    echo $html; // ◄■■ DISPLAY THE WEBPAGE.
    ?>
      </body>
    </html>
    

    The Snoopy Class is only one file, make sure the file is in the same directory your PHP file is.

    点赞 评论 复制链接分享

相关推荐