2016-12-16 60 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