好吧所以我有一个网站的源代码我试图从以下源代码中获取此信息0.00:
<div class="row">
<div class="span10">
<div id="conn_status"/><br/>
<div id="balance_notifications"/>
</div>
</div>
<center>
<h2>0 RLM
(0.00 USD)</h2>
我希望能够同时获取0确实改变的0 RLM值 而且我也想抓住0.00美元部分我只需要在我的php文件中的echo输出中显示的数字
这里我到目前为止
<?php
function check($url2){
$check = curl_init();
curl_setopt($check, CURLOPT_COOKIEJAR, 'cookiemon.txt');
curl_setopt($check, CURLOPT_COOKIEFILE, 'cookiemon.txt');
curl_setopt($check, CURLOPT_TIMEOUT, 40000);
curl_setopt($check, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($check, CURLOPT_URL, $url2);
curl_setopt($check, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($check, CURLOPT_FOLLOWLOCATION, false);
return curl_exec ($check);
curl_close ($check);
}
$html = check('https://example.org/');
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$value = $xpath->query('//label[@id="balance_notifications"]/following-sibling::text()')->item(0)->nodeValue;
?>
这是我得到的php响应
注意:尝试在第20行的/opt/lampp/htdocs/newphp.php中获取非对象的属性
答案 0 :(得分:0)
您的XPath表达式错误,这就是为什么没有第一个元素(根本没有)。
如果这是您内容中唯一的<h2/>
元素,请使用//h2
作为XPath表达式。
如果你想在<div id="balance_notifications"/>
之后查询第一个,你可以使用这个表达式(和其他人一样,但你没有提供足够的输入信息):
//div[@class="row"][.//div[@id="balance_notifications"]]//h2
如果您不能依赖<div class="row"/>
,请使用following::h2
并限制第一个结果:
//div[@id="balance_notifications"]/following::h2[1]
关于字符串操作:你在PHP中使用的XPath 1.0不能这样做,所以你必须在PHP中将它拆分。