gpt4 book ai didi

php - dl dd 的 xpath 查询不起作用

转载 作者:行者123 更新时间:2023-12-02 05:22:17 25 4
gpt4 key购买 nike

这是我到目前为止的代码:

$htmlString = file_get_contents("http://developer.adition/rAD41126");
$domDocument = new DOMDocument();
$domDocument->loadHTML($htmlString);
$xpath = new DOMXPATH($domDocument);
$className = "phui-property-list-properties ";
$results = $xpath->query("//dl[@class='$className']/dd[last()]" );

var_dump($results);`

输出是一个空的DOMNodeList元素。它应该给我html部分的最后一个dd元素,如下所示。类名 "phui-property-list-properties " 在 DOM 中出现时故意有一个额外的空格。

具有相关元素的 DOM 部分是

<dl class="phui-property-list-properties ">
<dt class="phui-property-list-key">Project/Package Auditors </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Committed </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Parents </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Branches </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Tags </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Subscribers </dt>
<dd class="phui-property-list-value">
<em>None</em>
</dd>
</dl>

我想获取带有属性 class="phui-property-list-value"

的最后一个 dd 元素

编辑

即使对于查询,我也得到一个空对象

$className = "phui-property-list-properties ";
$results = $xpath->query("//dl[@class='$className']");

var_dump($results);

最佳答案

DOM 对象对于 var_dump()print_r() 是不可见的。 Read Here

您的代码工作正常并且还显示结果数量,但 var_dump() 在这里不起作用。下面的代码展示了如何检查结果。

<?php
$htmlString = <<<EOD
<dl class="phui-property-list-properties ">
<dt class="phui-property-list-key">Project/Package Auditors </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Committed </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Parents </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Branches </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Tags </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Subscribers </dt>
<dd class="phui-property-list-value">
<em>None</em>
</dd>
</dl>
EOD;

$domDocument = new DOMDocument();
$domDocument->loadHTML($htmlString);
$xpath = new DOMXPATH($domDocument);
$className = "phui-property-list-properties ";
$results = $xpath->query("//dl[@class='$className']/dd[last()]" );

foreach ($results as $result) {
$xml = $result->ownerDocument->saveXML($result);
var_dump($xml);
}
?>

输出:

string(74) "<dd class="phui-property-list-value">&#13;
<em>None</em>&#13;
</dd>"

关于php - dl dd 的 xpath 查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26361710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com