Mohamed Ahmed
Mohamed Ahmed

Reputation: 35

InvalidArgumentException in Crawler.php line 516: The current node list is empty. with laravel-goutte

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

Upvotes: 0

Views: 1010

Answers (1)

Hyder B.
Hyder B.

Reputation: 12186

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..
}

Upvotes: 2

Related Questions