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 . "
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度