【问题标题】:InvalidArgumentException: The current node list is empty. PHP-Spider (DOMCrawler Symfony)InvalidArgumentException:当前节点列表为空。 PHP-Spider (DOMCrawler Symfony)
【发布时间】:2014-03-17 00:37:42
【问题描述】:

我正在使用 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 但没有运气

【问题讨论】:

标签: php symfony web-crawler


【解决方案1】:

是的,count() 方法可以用于此,像这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多