goutteをwhileループ内で使用しようとしたとき、goutteインスタンスが1回だけ作成され、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;
}
その後、私はあなたがあなた自身の統合(Lavavelでグット)を使用して、その理由を見つけるためにあなたのGoutte::request()
を見てくださいされているwhileループ
ok ...確かにそれはgoutteの問題でした....私はあなたの助言を念頭に置くでしょう。コメントありがとう。私は今それを解決しました。 – itsdenty