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

前端之家收集整理的这篇文章主要介绍了php – 如何使用Goutte刮擦laravel 5.2?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是Laravel 5.2的新手,我想抓一个网页.我开始知道它可以通过使用 Goutte完成.并且在不知道如何使用它.

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

解决方法

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

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

在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>';
    }
  }
}

猜你在找的Laravel相关文章