[英]DOMXPath $html->query('//p[@class=“myclass”]/a')->item(0); not working
DOMXPath $html->query('//p[@class="myclass"]/a')->item(0);
不管用。
这是 HTML:
<p class="username">
<img src="http://static1.tryal.com/img/b.gif" onclick="lalalaa()" class="a12s_iYblt4hN_DkIt_GoiyOtu8 opsi" />
<b>
<a href="/about"><b>Lalala.</b></a>
</b>
</p>
$name = $html->query('//p[@class="username"]/a')->item(0)->nodeValue;
//This doesn't return the name "Lalala.";
$name = $html->query('//p[@class="username"]')->item(0)->nodeValue;
//This works just fine.
为什么这棵树不工作? 我打错了吗?
非常感谢您提前。
您指定的 xpath 没有提供任何结果(或者没有准确的元素的结果)。 您可以通过不让b
元素滑过来解决此问题:
$name = $html->query('//p[@class="username"]/b/a')->item(0)->nodeValue;
^^^
或者不a
p
直接/直接子代,而是在更深的地方(xpath 中的双斜杠//
的后代):
$name = $html->query('//p[@class="username"]//a')->item(0)->nodeValue;
^^
尝试
$name = $html->query('//p[@class="username"]//a')->item(0)->nodeValue;
你正在寻找(单斜杠)'p'的'a'孩子,而你想要我理解的是(双斜杠)'p'的'a'后代
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.