[英]Xpath query assistance nested anchor href extract
这是我的代码:
$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');
我正在尝试在div cgus_post
获取锚链接的href文本。 我的查询有什么问题?
可能缺少“@”
'//div[@class="cgus_post"]//@href'
XPath不正确。 它应该是:
//div[@class="cgus_post"]/a
那么$nodes
将是<div class="cgus_post">
中所有<a>
标签的列表,并且你得到它们的hrefs
foreach($nodes as $node) {
$href = $node->getAttribute('href');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.