加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 如何使用Goutte刮擦laravel 5.2?

发布时间:2020-12-14 19:35:55 所属栏目:大数据 来源:网络整理
导读:我是Laravel 5.2的新手,我想抓一个网页.我开始知道它可以通过使用 Goutte完成.并且在不知道如何使用它. 我已经安装了Laravel和Goutte,但是如何使用呢?如何设置控制器,路由和所有需要的东西? 解决方法 我找到了答案. 我只是添加URL来路由并创建控制器 Route
我是Laravel 5.2的新手,我想抓一个网页.我开始知道它可以通过使用 Goutte完成.并且在不知道如何使用它.

我已经安装了Laravel和Goutte,但是如何使用呢?如何设置控制器,路由和所有需要的东西?

解决方法

我找到了答案.
我只是添加URL来路由并创建控制器

Route::resource('scrape','WebScraperController@index');

在WebScraperController内部

<?php

  namespace AppHttpControllers;

  use IlluminateHttpRequest;
  use GoutteClient;
  use SymfonyComponentDomCrawlerCrawler;
  use AppHttpRequests;
  class WebScraperController extends Controller
  {
  public function index()
  {
  //  Create a new Goutte client instance
    $client = new Client();

 //  Hackery to allow HTTPS
    $guzzleclient = new GuzzleHttpClient([
        'timeout' => 60,'verify' => false,]);

    // Create DOM from URL or file
    $html = file_get_html('https://www.facebook.com');

    // Find all images
    foreach ($html->find('img') as $element) {
        echo $element->src . '<br>';
    }

    // Find all links
    foreach ($html->find('a') as $element) {
        echo $element->href . '<br>';
    }
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读