Php goutte http请求未创建多个实例
当我尝试在while循环中使用gout时,gout实例只创建了一次,现在重复了20次,因为我希望每个循环都有一个新实例。过滤掉的数据的结果是第一个实例上的数据重复二十次,而我想要的是所有20页上的独立数据Php goutte http请求未创建多个实例,php,laravel,api,guzzle,goutte,Php,Laravel,Api,Guzzle,Goutte,当我尝试在while循环中使用gout时,gout实例只创建了一次,现在重复了20次,因为我希望每个循环都有一个新实例。过滤掉的数据的结果是第一个实例上的数据重复二十次,而我想要的是所有20页上的独立数据 while($count <=20) { $new_url = $url .$count; $check[] = $new_url; //get a goutte object of each new url returned after
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($count filter($lin)->每个函数($node,$i){
返回$node->text();
});
$pattern='tr>td.pu>a';
//获取a类表数据内的所有链接
$links=$crawler->filter($pattern)->each(函数($node,$i){
$href=$node->extract(array('href');//这是一个doElement对象
返回$href;
});
//筛选所需链接,该链接始终大于30个字符
foreach($links作为$link){
如果(strlen($link[0])>30){
$p_links[]=$link;
}
}
对于($i=0;$i$results[$i],'links'=>'http://www.nairaland.com“.$p_links[$i][0]];
}
//将数据添加到数组中
$data[]=$content;
$count++;
$crawler=null;
}
然后我返回了while循环之外的数据,您正在使用自己的集成(Lavavel中的Goutte),因此请查看您的
Goutte::request()
,以查找原因
另外,为了简化对问题的理解,请在将来只包含相关代码(我认为循环中的大多数代码与本文中的问题无关,但可能我错了).我最终能够解决这个问题,方法是将循环中的整个goutte代码移动到另一个函数,然后在循环中调用该函数。这是因为每个goutte实例都是在循环中的每个函数调用中独立创建和使用的。好吧……事实上,这更像是一个goutte问题……我会记住你的建议谢谢你的评论。我现在已经解决了。