【问题标题】:PHP can't return or echo (Goutte)PHP 无法返回或回显(Goutte)
【发布时间】:2021-04-18 06:00:36
【问题描述】:

我在使用 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,但没有成功。我在这里做错了什么?

【问题讨论】:

  • 如果那是您的控制器,您需要返回包含数据的视图。控制器不回显或显示数据。
  • 它不是控制器,但我在控制器中调用了这个函数。这实际上必须作为 json 响应返回,所以当我在 Postman 中运行它时,它会显示所有内容。

标签: php laravel web-scraping goutte


【解决方案1】:

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

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

      var_dump($title) ;
    }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 2012-06-10
    • 2020-03-04
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多