标签,
有 3 个 < p> 标签和
有 2 个
标签。我需要提取
class. 中的
标签
<body>
<div class="text">
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
</div>
<div class="another-text">
<p>...</p>
<p>...</p>
<p>...</p>
</div>
<div class="another-another-text">
<p>...</p>
<p>...</p>
</div>
</body>
由于 php 中的 xpath 支持限制,您将不得不求助于:
$html=
'[your html above]
';
$HTMLDoc = new DOMDocument();
$HTMLDoc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
$xpath = new DOMXPath($HTMLDoc);
#locate the 3 divs
$pees = $xpath->query('//div[.//p]');
$pchilds = [];
#get the number of p children in each div
foreach ($pees as $pee) {
$childs = $pee->childElementCount;
array_push($pchilds,$childs);}
#now find the div with the max number of p children
foreach ($pees as $pee) {
$childs = $pee->childElementCount;
if ($childs == max($pchilds))
echo ($pee->nodeValue);
#or do whatever
}
仅供参考,在 xpath 2.0 支持下,这可以用一行 xpath 表达式来完成:
//div[count(.//p) = max(//div/count(.//p))]
如果 div 在同一个父级中,则可以通过此 XPath 完成:
//div[count(p) > count(preceding-sibling::div/p) and count(p) > count(following-sibling::div/p)]
如果 div 不在同一个父元素中,可以通过这个 XPath 来完成:
//div[count(p) > count(preceding::div/p) and count(p) > count(following::div/p)]