我需要一些xpath的帮助来从html页面中提取$ 283的值.
这是我的PHP
$html = file_get_contents("https://test.com/testpage.php");
$html = tidy_repair_string($html);
$doc = new DomDocument();
$doc->loadHtml($html);
$xpath = new DomXPath($doc);
$avg = $xpath->evaluate('string(//*[@id="Average"]/@value)');
echo $avg;
Run Code Online (Sandbox Code Playgroud)
这是HTML
<div class="List">
<span class="bold">Avg</span>
<span id="Average">$283</span>
</div>
Run Code Online (Sandbox Code Playgroud)
提前致谢.
试试这个XPath表达式:
//div[@class="List"]/span[@id="Average"]
Run Code Online (Sandbox Code Playgroud)
或者干脆:
//*[@id='Average']
Run Code Online (Sandbox Code Playgroud)
这是一个完整的代码:
<?php
$doc = new DOMDocument();
$doc->loadHTMLFile('http://127.0.0.1/test.html');
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*[@id='Average']");
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
只需用您自己的URL替换URL即可.
| 归档时间: |
|
| 查看次数: |
4006 次 |
| 最近记录: |