Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scrape in laravel 5.2 using Goutte?

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?

like image 962
Aman Kumar Avatar asked Dec 11 '22 18:12

Aman Kumar


1 Answers

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>';
    }
  }
}
like image 186
Aman Kumar Avatar answered Dec 15 '22 00:12

Aman Kumar