$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');
...
...
}
答案 0 :(得分:1)
问题是Crawler
个实例(存储在$rows
,$reviewContent
等中)会在其DOMElement内容上进行迭代。您可以在Crawler
的每个循环步骤中创建一个新的DOMElement
实例,如下所示:
foreach ($rows as $domRow) {
$row = new Crawler($domRow);
// proceed as in the original code
}