我正在使用 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。
有没有办法检查它是否存在?
我试过 isset & !empty 但没有成功
最佳答案
是的,count()
方法可以用于此目的,如下所示:
if( $resource->getCrawler()->filterXpath('//title')->count() )
{
echo $resource->getCrawler()->filterXpath('//title')->text() . ', ';
}
关于php - InvalidArgumentException : The current node list is empty. PHP-Spider (DOMCrawler Symfony),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22445102/