<?php
use Symfony\Component\DomCrawler\Crawler;
// require dependencies here
$html = <<<'HTML'
<!DOCTYPE html>
<html>
<body>
<p class="message">Hello World!</p>
<p>Hello Crawler!</p>
<div id="product">
<a class="bla">
<img src="images/ok.png">
</a>
</div>
</body>
</html>
HTML;
$crawler = new Crawler($html);
$link = $crawler->filterXPath('//a[contains(@class,"bla")]');
echo var_dump(count($link));
var_dump($link->filterXPath('//img')->attr('src'));
执行结果:
int(1)
string(13) "images/ok.png"
从上述示例我们可以看出$link返回的是一个数组,我们可以通过count($link)判断节点是否存在,即通过count($link)与0进行比较。
<?php
use Symfony\Component\DomCrawler\Crawler;
// require dependencies here
$html = <<<'HTML'
<!DOCTYPE html>
<html>
<body>
<p class="message">Hello World!</p>
<p>Hello Crawler!</p>
<div id="product">
<a class="dvy">
<img src="images/ok.png">
</a>
</div>
</body>
</html>
HTML;
$crawler = new Crawler($html);
$link = $crawler->filterXPath('//a[contains(@class,"dvy")]');
echo var_dump(count($link));
var_dump($link->filterXPath('//img')->attr('src'));
if(count($crawler->filterXPath('//a[contains(@class,"dvy")]'))<=0){
echo '查询结果小于等于0';
}else{
echo '查询结果存在';
}