InvalidArgumentException当前节点列表为空
我正在尝试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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试图访问空节点列表的文本时会引发此异常。我已经为此目的写了一个UTIL课程,因为我并不总是介意价值是空的。只需传递您想要从中获得内部文本的节点列表,例如So
crawlutil :: innertext($ nodelist)
。希望将来会对某人有所帮助:)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 :)