【问题标题】:Attempted to call an undefined method named "filter" of class "DOMElement"试图调用类“DOMElement”的名为“filter”的未定义方法
【发布时间】:2015-08-11 18:51:37
【问题描述】:
$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');
    ...
    ...
}

【问题讨论】:

  • @raina77ow 你想把它作为答案吗?
  • 是的,如果有效的话;不能在这里检查,很遗憾。 )
  • @raina77ow。你的评论完美无缺,刚刚拯救了我的后路。如果是答案,我可以标记它!

标签: php goutte domcrawler


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2015-12-20
    相关资源
    最近更新 更多