xpath无法获取锚点href值
问题描述:
我遇到的问题是从xpath query
获取anchor href
。xpath无法获取锚点href值
下面是该查询:
$hrefxpath = $xpath->query('//*[@id="content"]/article[1]/article[1]/div/h2/a[@href]');
foreach($hrefxpath as $item){
var_dump($item->nodeValue);
}
下面是HTML:
<div id="content">
<article>
<article>
<div>
<h2>
<a href="hrefvalue">Something Awesome</a>
</h2>
</div>
</article>
</article>
</div>
结果是Something Awesome
但没有href的值。
任何建议将不胜感激。
答
也就是说,因为您只是输出nodeValue。添加
var_dump($item->getAttribute('href'));
获取DOMElement的属性。