简单的xpath查询不起作用 [英] simple xpath query not working

查看:215
本文介绍了简单的xpath查询不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码段不起作用:

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

但是xpath查询显然是正确的...并且提供的url显然有一个标记。
我试图用 // html替换查询,但是没有运气。
我总是使用xpath,这是一个奇怪的行为。

but the xpath query seems obviously correct... and the url provided obviously have a tag . I tried to replace the query even with '//html' but no luck. I always use xpath and this is a strange behaviour.

<?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;
?>


推荐答案

xpath查询没有问题正确的语法,并且该节点确实存在。有问题的行是这样的:

There is nothing wrong with your xpath query as it is correct syntax and the node does exist. The problematic line is this:

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

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

You are not loading the result page that you got from your curl request properly. To load the response,

请改用此命令:

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

这是您期望的示例输出

这篇关于简单的xpath查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆