【问题标题】:simple xpath query not working简单的 xpath 查询不起作用
【发布时间】:2014-12-09 22:11:18
【问题描述】:

这个sn-p的代码不起作用:

Notice: Trying to get property of non-object in test.php on line 13

但 xpath 查询似乎显然是正确的......并且提供的 url 显然有一个 tag 。 我尝试用“//html”替换查询,但没有运气。 我总是使用 xpath,这是一种奇怪的行为。

<?php
$_url = 'http://www.portaleaste.com/it/Aste/Detail/876989';

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $_url);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$result2 = curl_exec($ch2);
curl_close($ch2);

$doc2 = new DOMDocument();
@$doc2->load($result2);
$xpath2 = new DOMXpath($doc2);
$txt = $xpath2->query('//p[@id="descrizione"]')->item(0)->nodeValue;
echo $txt;
?>

【问题讨论】:

  • 如果您确定哪一行是第 13 行,那将会很有用!!!!
  • 您确定不需要指定命名空间吗?
  • @RiggsFolly 我想我们可以推断它是带有 XPath 表达式的行!
  • @JKesMc9tqIQe9M 我们几乎都知道假设是所有错误之母,以保持礼貌。提问者有责任让事情尽可能清楚。

标签: php html curl xpath domdocument


【解决方案1】:

您的 xpath 查询没有任何问题,因为它是正确的语法并且节点确实存在。有问题的行是这样的:

@$doc2->load($result2);
// DOMDocument::load — Load XML from a file

您没有正确加载从 curl 请求中获得的结果页面。要加载响应,

改用这个:

@$doc2->loadHTML($result2);
// DOMDocument::loadHTML — Load HTML from a string

Here's a sample output you'd expect

【讨论】:

  • @MarcoMarsala 是的,非常微妙但很重要。很高兴这有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多