首页 > 解决方案 > PHP 无法返回或回显(Goutte)

问题描述

我在使用 Goutte 抓取数据时遇到了一些困难。我的功能如下:

public function getTitle(){
        $this->crawler->filter('script')->each(
            function ($node) {
                print $node->text();
        });
    }

这实际上会返回我需要的东西,但无论如何它都会返回它,即使我不想在网页上回显或打印。但是当我尝试这样的事情时:

public function getTitle(){
        $this->crawler->filter('script')->each(
            function ($node)  use (&$title) {
                $title = $node->text();
        });

      return $title; // or echo or print
    }

它没有返回任何东西。我尝试在爬虫之前定义 $title,但没有成功。我在这里做错了什么?

标签: phplaravelweb-scrapinggoutte

解决方案


我想这就是你的问题可以解决的方法。你只是得到了各种各样的参数,你需要存储每个参数而不是覆盖它们。

public function getTitle(){
        $title =$this->crawler->filter('script')->each(function ($node){
                return $node->text();
        });

      var_dump($title) ;
    }

见: https ://github.com/FriendsOfPHP/Goutte/issues/266


推荐阅读