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
Yes, count()
method can be used for that, like this:
if( $resource->getCrawler()->filterXpath('//title')->count() )
{
echo $resource->getCrawler()->filterXpath('//title')->text() . ', ';
}
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