2016-12-16 63 views
-1

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

回答

0

我最終能夠通過將循環內的整個goutte代碼移動到另一個函數然後調用循環內的函數來解決此問題。這是因爲每個goutte實例都是在循環內部的每個函數調用中獨立創建和使用的。

0

您正在使用您自己的集成(GOUTTE在Lavavel)之外的數據,所以請看看你的Goutte::request()上找原因。爲了簡化對問題的理解(我認爲循環中的大多數代碼沒有連接到這篇文章中的問題,但也許我錯了),請在將來僅包含相關代碼。

+0

確定....確實這是更多的痛苦問題....我會把你的建議記在腦海中。感謝您的評論。我現在已經解決了。 – itsdenty