0

why when I execute the code in the document of the laravel-goutte it doesn't work , that code is in the main page of the package on github:

https://github.com/dweidner/laravel-goutte

use Weidner\Goutte\GoutteFacadeGoutte;


Route::get('/', function() {
    $crawler = Goutte::request('GET', 'http://duckduckgo.com/?q=Laravel');
    $url = $crawler->filter('.result__title > a')->first()->attr('href');
    dump($url);
    return view('welcome');
});

and shows that error

error message

I use laravel 2.2.29

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mohamed Ahmed
  • 35
  • 1
  • 8

1 Answers1

2

Your filterdid not return any results. That is why it crashed. That's how I solved this issue, by adding a try catch.

try {
   $url = $crawler->filter('.result__title > a')->first()->attr('href');
} catch (\InvalidArgumentException $e) {
    // Handle the current node list is empty..
}
Hyder B.
  • 10,900
  • 5
  • 51
  • 60