未定义的属性:DOMNodeList :: $ textContent何时解析web

时间:2015-05-20 07:58:58

标签: php xpath

在我的code1中,它可以解析网络以获取td内容。

CODE1

<?php
    $url='http://www.sse.com.cn/marketservices/tradingservice/shhksc/eligible/';
    $html =  file_get_contents($url);
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    $nodes = $xpath->query('//div[@id="hk_view"]//table[@class="tablestyle"]//tr//td[position()<4 and position()>1]');
    foreach($nodes as $node){
        echo $node->textContent.'</br>';}     
?>

现在我改变其他格式来解析网络。

码2

<?php
    $url='http://www.sse.com.cn/marketservices/tradingservice/shhksc/eligible/';
    $html =  file_get_contents($url);
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    $nodes = $xpath->query('//div[@id="hk_view"]//table[@class="tablestyle"]//tr');
    foreach($nodes as $node){
        $sub =$xpath->query('//td[position()<4 and position()>1]' ,$node);
        echo $sub->textContent.'</br>';}

?>

xpath表达式是错误的吗?

$sub =$xpath->query('//td[position()<4 and position()>1]' ,$node);

这是我的代码1的结果。

enter image description here

根据har07的回答,code2被重写为code3,还有另外一个问题,请用我的code3进行测试。

CODE3

<?php
$url='http://www.sse.com.cn/marketservices/tradingservice/shhksc/eligible/';
$html =  file_get_contents($url);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//div[@id="hk_view"]//table[@class="tablestyle"]//tr');
foreach($nodes as $node){
    $sub =$xpath->query('//td[position()<4 and position()>1]' ,$node);
    foreach($sub as $s){
    echo $s->textContent.'</br>';
    }
}
?>

enter image description here

1 个答案:

答案 0 :(得分:1)

问题不在您使用的xpath表达式中。如错误消息所示,query()会返回DOMNodeList,但textContent没有DOMNode属性。 textContent DOMNodeListDOMNode

您需要遍历textContent以访问其个人DOMNode成员,并访问每个foreach($nodes as $node){ $sub = $xpath->query('.//td[position()<4 and position()>1]' ,$node); foreach($sub as $s){ echo $s->textContent; } } 上的 @RolesAllowed("StandardRole") 192.168.0.132/download.ws/fileid 媒体资源:


@RolesAllowed("SuperUser")
192.168.0.132/download.ws/{special_path_pattern}/fileid