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?
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>';
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With