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

1 回答 1

4

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

foreach ($rows as $domRow) {
  $row = new Crawler($domRow);
  // proceed as in the original code
}
于 2016-05-27T09:42:10.377 回答