当我试图在while循环中使用goutte时,goutte实例只创建一次,现在重复20次,因为我希望每个循环都有一个新实例。过滤出的数据的结果是第一次重复数据20次,因为我想要的是所有20页上的单独数据。goutte http请求不创建多个实例
while($count <=20) {
$new_url = $url .$count;
$check[] = $new_url;
//get a goutte object of each new url returned after each loop
$crawler = Goutte::request('GET', $new_url);
//get all text from a table data of class narrow
$results = $crawler->filter($lin)->each(function ($node, $i) {
return $node->text();
});
$pattern = 'tr>td.pu>a';
//get all the links inside table data of class a
$links = $crawler->filter($pattern)->each(function ($node, $i) {
$href = $node->extract(array('href')); // This is a DOMElement Object
return $href;
});
//filter the links for the needed one which is always greater than 30 characters
foreach($links as $link){
if(strlen($link[0]) > 30){
$p_links[] = $link;
}
}
for($i =0; $i<count($results)-3; $i++){
$content[] = ['comments' => $results[$i], 'links' => 'http://www.nairaland.com' . $p_links[$i][0]];
}
//add the data to an array
$data[] = $content;
$count++;
$crawler = null;
}
然后我回到while循环
确定....确实这是更多的痛苦问题....我会把你的建议记在脑海中。感谢您的评论。我现在已经解决了。 – itsdenty