How to scrape in laravel 5.2 using Goutte?

Aman Kumar picture Aman Kumar · Feb 21, 2016 · Viewed 10.8k times · Source

I am new to Laravel 5.2, I want to scrape a web page. I came to know that it can be done by using Goutte. And in don't know how to use it.

I have installed Laravel and Goutte, But how to use it? How to set Controller, route and all things which are required?

Answer

Aman Kumar picture Aman Kumar · Feb 21, 2016

I found my answer. I just add the url to route and created the controller

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

Inside the 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>';
    }
  }
}