DOM分析器中的警告file_get_contents

时间:2018-10-03 13:54:30

标签: php curl domparser

我的情况是,我想抓取一个成功的网站,并且正在使用PHP cURL。当我想使用DOM解析器来获取我想要的内容时,问题就开始了。这是警告发出:

the error image is here

我使用的代码在这里。在此代码之前,我使用cURL抓取了一个网站,它可以正常工作,但是仅此部分出现了错误:

include 'simple_html_dom.php';

//Here is where I scraping, no need to show it

$fp = fopen(dirname(__FILE__) . '/airpaz.html', 'w');

//$html contain the page I scrap

fwrite($fp, $html);
fclose($fp);

$html_content = file_get_contents(dirname(__FILE__) . '/airpaz.html');

echo $html_content;

$html2 = new simple_html_dom();
$html2->load_file($html_content);

希望你们能有所帮助,谢谢

2 个答案:

答案 0 :(得分:0)

尝试此代码

include 'simple_html_dom.php';

$html_content = file_get_html(dirname(__FILE__) . '/airpaz.html');

echo $html_content;

$html2 = new simple_html_dom();
$html2->load_file($html_content);

答案 1 :(得分:0)

您似乎试图读取文件3次:

$read_file = fread($fr, filesize(dirname(__FILE__) . '/airpaz.html'));

和:

$html_content = file_get_contents($read_file);

和:

$html2->load_file($html_content);

在最后两个实例中,您将HTML内容传递给该函数,而不是文件名,这样将无法正常工作。

您应该只读取一次文件,并在收到的内容上使用字符串函数。或者您直接在$html2->load_file()中打开网址。