InvalidArgumentException当前节点列表为空

发布于 2025-02-05 05:27:35 字数 618 浏览 4 评论 0原文

我正在尝试Web刮擦,因此我在“ \ vendor \ symfony \ dom-crawler \ crawler.php:552”上遇到此错误 这是浏览器向我展示的crawler.php代码:

  • @throws \ invalidargumentException当当前节点为空时

    */

    公共功能文本(字符串$ default = null,bool $ normalizeWhitespace = true):字符串

    {

      if(!$ this-> nodes){
    
         如果(null!== $ default){
    
             返回$ default;
    
         }
    
    
    
         抛出新的\ invalidargumentException(“当前节点列表为空。”);
    
     }
    
    
    
     $ text = $ this-> getNode(0) - > nodeValue;
    
    
    
     if($ normalizeWhitespace){
    
         返回trim(preg_replace('/(?:\ s {2,}+| [^\ s])/',',$ text));
    
     }
    
    
    
     返回$ text;
     

    }

i am trying web scraping so i get this error on "\vendor\symfony\dom-crawler\Crawler.php:552"
here is the crawler.php code what the browser showed me:

  • @throws \InvalidArgumentException When current node is empty

    */

    public function text(string $default = null, bool $normalizeWhitespace = true): string

    {

     if (!$this->nodes) {
    
         if (null !== $default) {
    
             return $default;
    
         }
    
    
    
         throw new \InvalidArgumentException('The current node list is empty.');
    
     }
    
    
    
     $text = $this->getNode(0)->nodeValue;
    
    
    
     if ($normalizeWhitespace) {
    
         return trim(preg_replace('/(?:\s{2,}+|[^\S ])/', ' ', $text));
    
     }
    
    
    
     return $text;
    

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2025-02-12 05:27:35

试图访问空节点列表的文本时会引发此异常。我已经为此目的写了一个UTIL课程,因为我并不总是介意价值是空的。只需传递您想要从中获得内部文本的节点列表,例如So crawlutil :: innertext($ nodelist)。希望将来会对某人有所帮助:)

use Symfony\Component\DomCrawler\Crawler;

class CrawlUtil
{

    /**
     * Suppresses exception that node list is empty.
     *
     * @param Crawler $crawler
     *
     * @return string|null
     */
    public static function innerText(Crawler $crawler): ?string
    {

        if ($crawler->count() > 0 && $crawler->text() !== '') {
            return $crawler->innerText();
        }

        return null;
    }

}

This exception is thrown when trying to access the text of an empty node list. I have written a util class for this purpose, as I don't always mind the value being empty. Simply pass the node list you want to get the inner text from, like so CrawlUtil::innerText($nodeList). Hope it will help somebody in the future :)

use Symfony\Component\DomCrawler\Crawler;

class CrawlUtil
{

    /**
     * Suppresses exception that node list is empty.
     *
     * @param Crawler $crawler
     *
     * @return string|null
     */
    public static function innerText(Crawler $crawler): ?string
    {

        if ($crawler->count() > 0 && $crawler->text() !== '') {
            return $crawler->innerText();
        }

        return null;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文