Xpath查询辅助嵌套锚点href提取

Here is my code:

$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');

I'm trying to get the href text for an anchor link within the div cgus_post. What's wrong with my query?

2个回答



可能缺少“@”</ p>

 '// div [@class =  “cgus_post”] // @ href'
</ code> </ pre>
</ div>

展开原文

原文

probably a missing "@"

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

dongxing1960
dongxing1960 喔! 我的新课程.. +1 ..是的,它的工作原理......谢谢! :)
接近 7 年之前 回复
douhe4336
douhe4336 会不会选择一个元素节点?
接近 7 年之前 回复

XPath is incorrect. It should be:

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

Then $nodes will be a list of all <a> tags inside a <div class="cgus_post">, and you get their hrefs with

foreach($nodes as $node) {
   $href = $node->getAttribute('href');
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐