0

好的,所以我有来自网站的源代码,我试图从以下源代码中获取此信息 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 中获取非对象的属性

4

1 回答 1

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 中将其拆分。

于 2013-07-29T16:48:48.300 回答