【问题标题】:goutte http request not creating multiple instancesgotte http请求不创建多个实例
【发布时间】:2017-05-02 01:06:01
【问题描述】:

当我尝试在 while 循环中使用 goutte 时,goutte 实例仅创建一次,现在重复 20 次,因为我希望每个循环都有一个新实例。过滤掉的数据的结果是第一个实例上的数据重复了 20 次,而我想要的是所有 20 页上的单独数据。

   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循环之外返回数据

【问题讨论】:

    标签: php laravel api guzzle goutte


    【解决方案1】:

    您正在使用自己的集成(Lavavel 中的 Goutte),因此请查看您的 Goutte::request() 以查找原因。

    另外,以后请只包含相关代码,以简化对问题的理解(我认为循环内的大多数代码都与本文中的问题无关,但也许我错了)。

    【讨论】:

    • 好的....确实这更像是一个痛风问题....我会记住您的建议。感谢您的评论。我现在已经解决了。
    【解决方案2】:

    我最终能够通过将循环内的整个 goutte 代码移动到另一个函数,然后在循环内调用该函数来解决这个问题。这是因为每个 goutte 实例都是在循环内的每个函数调用中独立创建和使用的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 2021-09-03
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      相关资源
      最近更新 更多