2016-12-16 17 views
-1

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ループ

答えて

0

私は最終的に、ループ内のgoutteコード全体を別の関数に移動し、ループ内で関数を呼び出すことでこれを解決することができました。各goutteインスタンスが作成され、ループ内の関数呼び出しごとに独立して使用されるようになった。

0

外のデータが返されました。

また、問題の理解を簡素化するために、関連するコードのみを含めてください(ループ内のほとんどのコードはこの記事の問題には接続していないと思いますが、間違いかもしれません)。

+0

ok ...確かにそれはgoutteの問題でした....私はあなたの助言を念頭に置くでしょう。コメントありがとう。私は今それを解決しました。 – itsdenty

関連する問題