簡體   English   中英

DOMXPath $html->query('//p[@class="myclass"]/a')->item(0); 不工作

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM