How can I create DOM object from a HTML file in php language?

醉酒当歌 提交于 2019-12-08 10:52:38

问题


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:


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.



来源:https://stackoverflow.com/questions/40426514/how-can-i-create-dom-object-from-a-html-file-in-php-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!