繁体   English   中英

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

[英]Attempted to call an undefined method named “filter” of class “DOMElement”

$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');
    ...
    ...
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM