获取错误尝试在简单的html dom中获取非对象的属性

时间:2017-03-11 05:37:25

标签: php simple-html-dom

这是我用来提取数据的代码:

ini_set("user_agent","Opera/9.80 (Windows NT 6.1; U; Edition Campaign 21; en-GB) Presto/2.7.62 Version/11.00");

$html14=file_get_html("https://www.gamesrocket.com/battlefield-1.html");

$title14 = $html14->find('span.price',0)->children(0)->plaintext;

for ($i=0; $i <sizeof($title14) ; $i++) { 
    # code...

    if($title14[$i]){
        echo $title14[$i]->plaintext;
        $a13=$title14[$i]->plaintext;
    }
    else{

        echo "problem";
    }

}

这里我试图从给定页面中提取价格,但我收到此错误:Trying to get property of non-object。我使用简单的html dom来提取数据

1 个答案:

答案 0 :(得分:0)

因为$ title14是字符串而不是对象, 你可以var_dump($title14);知道它的类型。

ini_set("user_agent","Opera/9.80 (Windows NT 6.1; U; Edition Campaign 21; en-GB) Presto/2.7.62 Version/11.00");



$html14=file_get_html("https://www.gamesrocket.com/battlefield-1.html");

 $title14 = $html14->find('span.price',0)->children(0)->plaintext;
  for ($i=0; $i <sizeof($title14) ; $i++) { 

   if($title14[$i]){
   echo $title14[$i];
   $a13=$title14[$i];
   }
  else{

    echo "problem";
}

}