InvalidArgumentException:当前节点列表为空.PHP-Spider (DOMCrawler Sym


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

我正在使用PHP-Spider抓取网站,但当它找不到。class时,它会抛出一个错误:InvalidArgumentException:当前节点列表为空。

代码如下:

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();
}

在某些页面上存在。buyfooter类,在其他页面上存在。listfooter类。

是否有办法检查它是否存在?

I've try isset &空但没有运气

是的,count()方法可以用于此,如:

if( $resource->getCrawler()->filterXpath('//title')->count() )
{
    echo $resource->getCrawler()->filterXpath('//title')->text() . ', ';
}