PHP fatal error : Cannot use object of type simple_html_dom as array

标签 php simple-html-dom

我正在使用 simple_html_dom 开发网络抓取应用程序.我需要提取网页中的所有图像。以下是可能性:

  1. <img>标记图片
  2. 如果有 <style> 的 css在同一页面中标记。
  3. 如果有内嵌样式的图片,带<div>或使用其他标签。

我可以使用以下代码抓取所有图像。

function download_images($html, $page_url , $local_url){

    foreach($html->find('img') as $element) {
        $img_url = $element->src;
        $img_url = rel2abs($img_url, $page_url);
        $parts   = parse_url($img_url);
        $img_path=  $parts['path'];
        $url_to_be_change = $GLOBALS['website_server_root'].$img_path;
        download_file($img_url, $GLOBALS['website_local_root'].$img_path);  
        $element->src=$url_to_be_change;            
    }

    $css_inline = $html->find("style");

    $matches = array();
    preg_match_all( "/url\((.*?)\)/", $css_inline, $matches, PREG_SET_ORDER );
    foreach ( $matches as $match )    {
        $img_url = trim( $match[1], "\"'" );
        $img_url = rel2abs($img_url, $page_url);
        $parts   = parse_url($img_url);
        $img_path=  $parts['path'];
        $url_to_be_change = $GLOBALS['website_server_root'].$img_path  ;
        download_file($img_url , $GLOBALS['website_local_root'].$img_path); 
        $html = str_replace($img_url , $url_to_be_change , $html );
    }

    return $html;
}

$html = download_images($html , $page_url , $dir); // working fine
$html = str_get_html ($html);
$html->save($dir. "/" . $ff);    

请注意,我也在图片下载后修改 HTML。

下载正常。但是当我尝试保存 HTML 时,出现以下错误:

PHP Fatal error: Cannot use object of type simple_html_dom as array

重要:如果我不使用 str_replace,它工作得很好和第二个循环。

Fatal error: Cannot use object of type simple_html_dom as array in /var/www/html/app/framework/cache/includes/simple_html_dom.php on line 1167

最佳答案

猜一号

我在这里看到一个可能的错误:

$html = str_get_html($html);

看起来您将一个对象传递给函数 str_get_html(),而它接受一个字符串作为参数。让我们这样解决:

$html = str_get_html($html->plaintext);

我们只能猜测出现在这段代码中的 $html 变量的内容是什么。

猜 2

或者也许我们只需要在函数 download_images 中使用另一个变量来使您的代码在这两种情况下都正确:

function download_images($html, $page_url , $local_url){

    foreach($html->find('img') as $element) {
        $img_url = $element->src;
        $img_url = rel2abs($img_url, $page_url);
        $parts   = parse_url($img_url);
        $img_path=  $parts['path'];
        $url_to_be_change = $GLOBALS['website_server_root'].$img_path  ;
        download_file($img_url , $GLOBALS['website_local_root'].$img_path); 
        $element->src=$url_to_be_change;            
    }

    $css_inline = $html->find("style");

    $result_html = "";
    $matches = array();
    preg_match_all( "/url\((.*?)\)/", $css_inline, $matches, PREG_SET_ORDER );
    foreach ( $matches as $match )    {
        $img_url = trim( $match[1], "\"'" );
        $img_url = rel2abs($img_url, $page_url);
        $parts   = parse_url($img_url);
        $img_path=  $parts['path'];
        $url_to_be_change = $GLOBALS['website_server_root'].$img_path  ;
        download_file($img_url , $GLOBALS['website_local_root'].$img_path); 
        $result_html = str_replace($img_url , $url_to_be_change , $html );
    }

    return $result_html;
}

$html = download_images($html , $page_url , $dir); // working fine
$html = str_get_html ($html);
$html->save($dir. "/" . $ff);

解释:如果没有匹配项(数组 $matches 为空),我们永远不会进入第二个循环,这就是为什么变量 $html 仍然具有与函数开头相同的值。当您尝试在需要两个不同变量的代码中使用同一个变量时,这是一个常见的错误。

关于PHP fatal error : Cannot use object of type simple_html_dom as array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29967598/

相关文章:

php - 使用 html 字符串通过 id 从 div 中提取内部文本

php - 单一强制索引在连接表上引发错误

php - 如何在 PHP 中识别用户语言

php - 警告 : file_get_contents: failed to open stream: Redirection limit reached, 中止

php - 从 php 数组中回显一个项目

mysql - 使用简单 HTML DOM 解析器解析远程 HTML 文件收到的值不会存储到 mysql 数据库中

javascript - PHP - 如果 div 类出现在页面上 - 隐藏单独的元素

php - 如何在 AWS linux 服务器上安装 ejabberd?

php - 是否有类似 &nbsp 的东西?

php - PHP 简单 HTML DOM 解析器的字符编码问题