【问题标题】:DOM not printing nodeValue of an idDOM 不打印 id 的 nodeValue
【发布时间】:2015-02-15 20:30:13
【问题描述】:

我正在打印 Google 以及 Google 网站的共享计数。我的编码:

$dom = new DOMDocument();
$html = strtolower(file_get_contents("https://plusone.google.com/u/0/_/+1/fastbutton?count=true&url=http://google.com"));
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
echo $dom->getElementById('aggregateCount')->nodeValue;

我得到这个输出:

Notice: Trying to get property of non-object in C:\xampp\htdocs\start\test.php on line 7

当我直接传递这个 html 时,它就可以工作了。

$html = "<html><head></head><body><div id='aggregateCount'>node value</div></body></html>";

【问题讨论】:

  • $dom 不是对象,因此,它可能无法正确加载 html
  • file_get_contents 是否成功返回您的 html?如果不是,则 dom 未加载到 $html 中,因此很想获得非对象的属性 nodeValue
  • 放弃strtolower 电话

标签: javascript php dom


【解决方案1】:

在执行 file_get_contents 时,您在 HTML 中使用 strtolower。但不是直接读取字符串。

【讨论】:

  • 是的,你是对的。我在应用 strtolower 函数 之后使用这个 id aggregateCount。我应该删除 strtolower 函数 否则将 strtolower 函数 应用于 id aggregateCount。谢谢
猜你喜欢
  • 2012-09-05
  • 2020-10-11
  • 2015-12-31
  • 1970-01-01
  • 2011-09-11
  • 2011-11-12
  • 2013-01-25
  • 1970-01-01
  • 2010-09-06
相关资源
最近更新 更多