PHP: Xpath查询类名和回显结果
PHP: Xpath query for classname and echo result
我有一个非常简单的 HTML 页面 (extract.html),内容如下:
<html>
<body>
<span class="price_value">
20.50
</span>
<table id="data" class="outer">
<tr><td>Happy</td><td>Sky</td></tr>
<tr><td>Happy</td><td>Sky</td></tr>
<tr><td>Happy</td><td>Sky</td></tr>
<tr><td>Happy</td><td>Sky</td></tr>
<tr><td>Happy</td><td>Sky</td></tr>
</table>
</body>
我想使用以下代码获取 class price_value (20.50) 的值:
<?php
$doc = new DOMDocument();
$doc->loadHTML("extract.html");
$doc->preserveWhiteSpace = false;
$finder = new DomXPath($doc);
$spaner = $finder->query("(//span[@class='price_value'])[1]");
print_r($spaner);
?>
然而,我收到的唯一输出是:
DOMNodeList Object ( [length] => 0 )
为什么找不到并打印我指定的 classname 的内容?
DOMDocument::loadHTML()
function expects HTML string parameter, not an HTML file name. You're expected to use DOMDocument::loadHTMLFile()
函数而不是从 HTML 文件加载:
$doc = new DOMDocument();
$doc->loadHTMLFile("extract.html");
.....
我有一个非常简单的 HTML 页面 (extract.html),内容如下:
<html>
<body>
<span class="price_value">
20.50
</span>
<table id="data" class="outer">
<tr><td>Happy</td><td>Sky</td></tr>
<tr><td>Happy</td><td>Sky</td></tr>
<tr><td>Happy</td><td>Sky</td></tr>
<tr><td>Happy</td><td>Sky</td></tr>
<tr><td>Happy</td><td>Sky</td></tr>
</table>
</body>
我想使用以下代码获取 class price_value (20.50) 的值:
<?php
$doc = new DOMDocument();
$doc->loadHTML("extract.html");
$doc->preserveWhiteSpace = false;
$finder = new DomXPath($doc);
$spaner = $finder->query("(//span[@class='price_value'])[1]");
print_r($spaner);
?>
然而,我收到的唯一输出是:
DOMNodeList Object ( [length] => 0 )
为什么找不到并打印我指定的 classname 的内容?
DOMDocument::loadHTML()
function expects HTML string parameter, not an HTML file name. You're expected to use DOMDocument::loadHTMLFile()
函数而不是从 HTML 文件加载:
$doc = new DOMDocument();
$doc->loadHTMLFile("extract.html");
.....