PHP DOM解析器错误

时间:2014-07-25 18:39:00

标签: php parsing dom fatal-error

我有解析网页的代码。包含解析每个页面的foreach。第一个循环正确执行,另一个循环在解析器中失败。致命错误:在第1129行的C:\ wamp \ www \ parser \ dom.php中调用非对象的成员函数find()。

Parser是PHP Simple HTML DOM Parser - http://sourceforge.net/projects/simplehtmldom/

<?php
header('Content-Type: text/html; charset=utf-8');
include ('dom.php');
$mysql = mysqli_connect("localhost", "root", "", "db");


$import['id'][]=3;
$import['url'][]='http://sitea.com';
$import['id'][]=2;
$import['url'][]='http://siteb.com';
$import['id'][]=1;
$import['url'][]='http://sitec.com';

for($i=0; $i<count($import['id']); $i++){
    $coupons=array();
    $id=0;
    $parser = new simple_html_dom();
    $parser->load_file($import['url'][$i]);

    foreach($parser->find('section[class=couponList]', 0)->find('article[class=coupon]') as $coupon){
        $coupons[$id]['name']=trim($coupon->find('div[class=name]',0)->plaintext);
        $date=explode(' ', trim($coupon->find('p[class=dateFormat]',0)->plaintext));
        $coupons[$id]['date-from']=strtotime(trim($date[45]));
        $coupons[$id]['date-to']=strtotime(trim($date[130]));
        $coupons[$id]['description']=trim($coupon->find('div[class=description]',0)->plaintext);
        $coupons[$id]['sale']=@trim($coupon->find('div[class=couponImageContainer] div p',0)->plaintext);
        $coupons[$id]['id']=$coupon->attr['data-id'];

        //Code
        if(trim($coupon->find('div[class=couponColorType]',0)->plaintext)=='Kód'){
            $parser2 = new simple_html_dom();
            $parser2->load_file($import['url'][$i].'?cpn_id='.$coupon->attr['data-id']);
            $coupons[$id]['code']=strip_tags($parser2->find('span[class=popupCode]',0));
            $coupons[$id]['type']="kod";
        }else{
            $coupons[$id]['type']="nabidka";
            $page=file_get_contents('http://slevovekody.com/r/'.$coupon->attr['data-id']);
            $data=explode("window.location = '", $page);
            $data=explode("';", @$data[1]);

            $u=explode('http://', $data[0]);
            if(count($u)>2){
                $u='http://'.$u['2'];
            }else{
                $u=$data[0];
            }
            $u=explode('?', $u);
            $coupons[$id]['url']=$u[0];
        }
        $id++;
    }
    foreach($coupons as $c){            
        if(mysqli_num_rows(mysqli_query($mysql, "SELECT * FROM coupon WHERE id_import='".$c['id']."'"))==0){
            mysqli_query($mysql, "INSERT INTO `coupon` (`eshop`, `id_import`, `name`, `alias`, `description`, `type`, `from`, `to`, `code`, `url`, `sale`) VALUES ('".$import['id'][$i]."', '".$c['id']."', '".$c['name']."', '".makeAlias($c['name'])."', '".$c['description']."', '".strtolower($c['type'])."', '".$c['date-from']."', '".$c['date-to']."', '".@$c['code']."', '".@$c['url']."', '".$c['sale']."')");
        }
    }
    var_dump($coupons);
    unset($coupons);
}

&GT;

错误

Fatal error: Call to a member function find() on a non-object in C:\wamp\www\parser\dom.php on line 1129
Call Stack
#   Time    Memory  Function    Location
1   0.0010  291880  {main}( )   ..\index.php:0
2   5.5803  21923208    simple_html_dom->find( )    ..\index.php:20

错误在线:

foreach($parser->find('section[class=couponList]', 0)->find('article[class=coupon]') as $coupon){

1 个答案:

答案 0 :(得分:0)

如果find()来电没有找到任何内容(例如没有匹配),则返回NULL。因此,您可以 NOT 链式查找()调用,因为任何失败的查找都将返回null并导致您的“非对象调用”。

e.g

$x = $dom->find('element_that_does_not_exist');  // $x becomes null
$x->find('body');

相当于做null->find()

您必须捕获中间结果并查看是否有任何匹配,例如

$x = $dom->find('element_that_does_not_exist');
if (!is_null($x)) {
   $y = $x->find('other stuff');
}