doudao2954 2014-07-07 09:01
浏览 21
已采纳

使用xpath和php从html页面检索数据

I know there are similar question, but, trying to study PHP I met this error and I want understand why this occurs.

<?php
    $url = 'http://aice.anie.it/quotazione-lme-rame/';
    echo "hello!
";
    $html = new DOMDocument();
    @$html->loadHTML($url);
    $xpath = new DOMXPath($html);
    $nodelist = $xpath->query(".//*[@id='table33']/tbody/tr[2]/td[3]/b");

    foreach ($nodelist as $n) {
        echo $n->nodeValue . "
";
    }
?>

this prints just "hello!". I want to print the value extracted with the xpath, but the last echo doesn't do anything.

  • 写回答

1条回答 默认 最新

  • doujiu3768 2014-07-07 09:34
    关注

    You have some errors in your code :

    1. You try to get the table from the url http://aice.anie.it/quotazione-lme-rame/, but it's actually in an iframe located at http://www.aiceweb.it/it/frame_rame.asp, so get the iframe url directly.

    2. You use the function loadHTML(), which load an HTML string. What you need is the loadHTMLFile function, which takes the link of an HTML document as a parameter (See http://www.php.net/manual/fr/domdocument.loadhtmlfile.php)

    3. You assume there is a tbody element on the page but there is no one. So remove that from your query filter.

    Working code :

    $url = 'http://www.aiceweb.it/it/frame_rame.asp';
    echo "hello!
    ";
    $html = new DOMDocument();
    @$html->loadHTMLFile($url);
    $xpath = new DOMXPath($html);
    $nodelist = $xpath->query(".//*[@id='table33']/tr[2]/td[3]/b");
    
    foreach ($nodelist as $n) {
        echo $n->nodeValue . "
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题