【发布时间】:2018-10-08 19:32:41
【问题描述】:
所以我试图通过 curl 和简单的 HTML dom 从这个网页 https://promo.pan.com.hr 获取一些信息。但不幸的是,我不断收到错误消息,我无法弄清楚出了什么问题......
<?php
include_once("simple_html_dom.php");
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$html = file_get_contents_curl("https://promo.pan.com.hr");
foreach($html->find("p") as $element)
echo $element->innertext . '<br>';
?>
有人知道我为什么会收到这个错误吗?
致命错误:未捕获的错误:调用 C:\Server\XAMPP\htdocs\pan\index.php:21 中字符串上的成员函数 find() 堆栈跟踪:#0 {main} 在 C:\Server 中抛出\XAMPP\htdocs\pan\index.php 在第 21 行
第 21 行是:
foreach($html->find("p") as $element)
【问题讨论】:
-
如果您发布完整的代码sn-p会更容易提供帮助。尝试打印
$html以确保正在读取文件?看起来你得到的是一个字符串而不是 HTML。 -
这是所有代码:)
-
编辑:几行没有显示,mb,现在显示所有内容
-
file_get_contents_curl 返回一个字符串,然后您尝试将其用作对象,这正是 PHP 所抱怨的。老实说,不要使用 simple_html_dom,它已经过时了,已经很久了,改用 DOMDocument。
$html = file_get_contents_curl("https://promo.pan.com.hr");$domd=@DOMDocument::loadHTML($html); foreach($domd->getElementsByTagName("p") as $element){ echo $element->textContent. '<br>'; }
标签: php curl dom html-parsing simple-html-dom