尝试调用类“DOMElement”的名为“filter”的未定义方法

时间:2015-08-11 18:51:37

标签: php goutte domcrawler

$goutte = new GoutteClient();
$crawler = $goutte->request('GET', 'https://www.website.com');
$reviewContent = $crawler->filter('.review-content');
$rows = $reviewContent->filter('.row');

foreach ($rows as $row) {
    $col1 = $row->filter('.col-md-3');
    $col2 = $row->filter('.col-md-9');
}

$col1

上给出错误

我可以使用此功能,但您无法使用break,因为它不是真正的for loop

$crawler->filter('.row')->each(function (Crawler $row, $i) {
    $col1 = $row->filter('.col-md-3');
    $col2 = $row->filter('.col-md-9');
    ...
    ...
}

1 个答案:

答案 0 :(得分:1)

问题是Crawler个实例(存储在$rows$reviewContent等中)会在其DOMElement内容上进行迭代。您可以在Crawler的每个循环步骤中创建一个新的DOMElement实例,如下所示:

foreach ($rows as $domRow) {
  $row = new Crawler($domRow);
  // proceed as in the original code
}