當我試圖在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