Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidArgumentException: The current node list is empty. PHP-Spider (DOMCrawler Symfony)

I'm using PHP-Spider to crawl a website but when it can't find a .class it throws an error: InvalidArgumentException: The current node list is empty.

The code is this:

foreach ($spider->getPersistenceHandler() as $resource) {
    echo $resource->getCrawler()->filterXpath('//title')->text().", ";
    echo $resource->getCrawler()->filterXpath("//meta[@name='description']/@content")->text()."<br>";
    echo $resource->getCrawler()->filterXpath("//div[@class='buyFooter']")->text();
    echo $resource->getCrawler()->filterXpath("//div[@class='listFooter']")->text();
}

on some pages the class .buyFooter exist and on others class .listFooter exist.

Is there a way to check if it exist?

I've tried isset & !empty but no luck

like image 578
DimitrisBor Avatar asked Mar 17 '14 00:03

DimitrisBor


1 Answers

Yes, count() method can be used for that, like this:

if( $resource->getCrawler()->filterXpath('//title')->count() )
{
    echo $resource->getCrawler()->filterXpath('//title')->text() . ', ';
}
like image 193
Banago Avatar answered Oct 23 '22 17:10

Banago