Goutte icon indicating copy to clipboard operation
Goutte copied to clipboard

The current node list is empty.

Open jigsawsoul opened this issue 7 years ago • 6 comments

Hi,

I'm getting this error, The current node list is empty.

Is there away of it just skipping this element if it's unable to scrape the data? e.g. $company_crawler->filter('a.cp-links-url')->attr('href')

Thanks.

jigsawsoul avatar Mar 05 '17 21:03 jigsawsoul

Hi,

you could wrap that line within an try catch block:

try{
    $company_crawler->filter('a.cp-links-url')->attr('href')
} catch(Exception $e) { // I guess its InvalidArgumentException in this case
    // Node list is empty
}

Or count the node list before you try to get that attribute:

if ($company_crawler->filter('a.cp-links-url')->count() > 0 ) {
    $cpLinkUrls = $company_crawler->filter('a.cp-links-url')->attr('href')
}

dpde avatar Mar 06 '17 10:03 dpde

Another way we can do this if fails, works for me.

<?php
$html = '<a href="#" class="cp-links-url">A link</a>';
if( strpos($html,'a.cp-links-url') ){ 
    $company_crawler->filter('a.cp-links-url')->attr('href');
}
?>

ghost avatar Jul 01 '17 14:07 ghost

Hi,

You could print the crawler to see if you have this href beacause I have the same problem but when I see the crawler I understand that I'm trying to select the erronous href

sachonidas avatar Aug 03 '17 07:08 sachonidas

Hi , I have this error when using pjax what can I do?

ngrcode avatar Sep 04 '18 05:09 ngrcode

Hi i have this error but I try the before code, but dont work for me 👎 Hi have this code: (when read one page with any pages) $table_details->children()->filter('tr')->each(function ( $col ){}); but when this dont see this filter in the list say this error (The current node list is empty), and I try validate to omit this or break, but dont work? I print the html code and the any table rows have th , how omit this?

andrwsv avatar Nov 20 '19 21:11 andrwsv

Hi all. Maybe this is not relevant, but my solution is to check the nodes. $crawler->filter returns a Crawler, it has a count() method which returns the number of node.

$data= $company_crawler->filter('a.cp-links-url')->attr('href');
$result= $data->count() ? $data->text() : 0;

Ainur777 avatar Apr 03 '22 18:04 Ainur777