如何安全地检查节点是否为空?(Symfony 2 爬虫)

编程入门 行业动态 更新时间:2024-10-28 14:26:31
本文介绍了如何安全地检查节点是否为空?(Symfony 2 爬虫)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我尝试从页面中获取一些不存在的内容时,我发现了这个错误:

When I try to take some nonexistent content from page I catch this error:

The current node list is empty. 500 Internal Server Error - InvalidArgumentException

如何安全地检查此内容是否存在?这里有一些不起作用的例子:

How can I safely check exists this content or not? Here some examples that does not work:

if($crawler->filter('.PropertyBody')->eq(2)->text()){ // bla bla } if(!empty($crawler->filter('.PropertyBody')->eq(2)->text())){ // bla bla } if(($crawler->filter('.PropertyBody')->eq(2)->text()) != null){ // bla bla }

谢谢,我帮助了自己:

$count = $crawler->filter('.PropertyBody')->count(); if($count > 2){ $marks = $crawler->filter('.PropertyBody')->eq(2)->text(); }

推荐答案

你有没有尝试过这样的事情?

Have you tried something like this?

$text = null; if (!empty($body = $crawler->filter('.PropertyBody'))) { if (!empty($node = $body->eq(2))) { $text = $node->text(); } } $this->assertContains('yourText', $text);

更多推荐

如何安全地检查节点是否为空?(Symfony 2 爬虫)

本文发布于:2023-07-30 04:38:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1246278.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:爬虫   节点   为空   Symfony

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!