2013-08-29 50 views
0

这里是我的代码:Xpath查询援助嵌套锚的href提取

$text = '<div class="cgus_post"><a href="?p=15055">Hello</a></div>'; 
$dom = new DomDocument(); 
$dom->loadHTML($text); 
$classname = 'cgus_post'; 
$finder = new DomXPath($dom); 
$nodes = $finder->query('//div[class="cgus_post"]//@href'); 

我想获得的股利cgus_post内的锚链接中的href的文字。我的查询有什么问题?

回答

2

可能丢失的 “@”

'//div[@class="cgus_post"]//@href' 
+0

'@ href'属性应该在标签'a'内。但是,如果不选择'a'元素节点,这个'// @ href'会如何工作? –

+0

哦!新的课程对我来说.. * + 1 * ..是这样工作..谢谢你! :) –

0

XPath不正确。它应该是:

//div[@class="cgus_post"]/a 

然后$nodes将是一个<div class="cgus_post">内的所有<a>标签的列表,你会得到他们的HREF与

foreach($nodes as $node) { 
    $href = $node->getAttribute('href'); 
}