如何使用Goutte刮擦laravel 5.2?

Ama*_*mar 3 php web-scraping goutte laravel-5.2

我是Laravel 5.2的新手,我想抓一个网页.我开始知道可以使用Goutte完成.并且在不知道如何使用它.

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

Ama*_*mar 12

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

Route::resource('scrape','WebScraperController@index');
Run Code Online (Sandbox Code Playgroud)

在WebScraperController内部

<?php

  namespace App\Http\Controllers;

  use Illuminate\Http\Request;
  use Goutte\Client;
  use Symfony\Component\DomCrawler\Crawler;
  use App\Http\Requests;
  class WebScraperController extends Controller
  {
  public function index()
  {
  //  Create a new Goutte client instance
    $client = new Client();

 //  Hackery to allow HTTPS
    $guzzleclient = new \GuzzleHttp\Client([
        '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>';
    }
  }
}
Run Code Online (Sandbox Code Playgroud)