试图提取Goutte数据进行查看

时间:2018-03-12 21:54:24

标签: php laravel web-crawler goutte

我已经工作了几个小时试图弄清楚如何在Laravel中提取一些数据到我的视图中。

我的控制器如下:

           MySqlDataReader reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                   outColumn.Add(reader[i].ToString());
                }
            }

然后我的观点是:

  public function practice(Request $request) 
    {

        $client = new Client();
        $crawler = $client->request('GET', 'https://www.polkadotpassport.com/');

$crawler->filter('.post-header')->each(function($node){
   $title = $node->filter('h2 > a');
    return view('flights', compact('title'));
});


    }

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

假设crawler正在返回一个集合,请尝试类似:

public function practice(Request $request)
{
        $client = new Client();
        $crawler = $client->request(
                'GET',
                'https://www.polkadotpassport.com/
        ');

        $titles = $crawler->filter('.post-header')->map(function($node){
                return $node->filter('h2 > a');
        });

        return view('flights', ['titles' => $titles]);
}