DOMXPath查询错误,无法在Web服务器中运行

时间:2011-11-12 16:30:39

标签: php domdocument domxpath

我使用以下代码从站点获取一些数据并将它们存储在数据库中。

<?php

error_reporting(0);
include("database.php");

$dom = new DOMDocument();
$url = "http://www.kitco.com";
$html = file_get_contents($url);
$dom->loadHTML($html);
$xp = new DOMXPath($dom);
get_live_spot_gold();
get_silver_and_pgm();

function get_live_spot_gold()
{
    global $xp;
    $live_spot_gold = array();

    $bid='000';
    $change='000';
    $qs = $xp->query('//div[@class="item_border"]/table/tr[@class="alternating"][1]/td[2]');
    foreach($qs as $q)
    {
            $bid = $q->textContent;
            break;

    }



    $qs = $xp->query('//div[@class="item_border"]/table/tr[@class="alternating"][2]/td[2]');
    foreach($qs as $q)
    {
        $change = $q->textContent;
        break;
    }



    //insert into db
    echo $bid."<br>";
    echo $change."<br>";
    $query = "UPDATE live_spot_gold SET _bid = '$bid' , _change = '$change'";
    echo $query;
    $result = mysql_query($query);
    if(!$result)echo "problem in live spot gold"."<br>";



}




function get_silver_and_pgm()
{
    global $xp;
    $silver_and_pgm = array();
    $cnt=0;
    $qs = $xp->query('//td[@id="right_column"]/div[@class="item_container"][2]/div[@class="item_border"]/table/tr');
    foreach ($qs as $q)
    {
        $line = $q->nodeValue;
        $demo="";
        for($i=0;$i<strlen($line);$i++)
        {
            if($line[$i]==' ')
            {
                $demo.=' ';
                for(  ;$line[$i]==' ';$i++);
            }
            $demo.=$line[$i];

        }
        $words = explode(" ",$demo);
        $silver_and_pgm[$cnt][0]=$words[0];  //metal name
        $silver_and_pgm[$cnt][1]=$words[1];  //bid
        $silver_and_pgm[$cnt][2]=$words[2];  //change
        $cnt++;
    }

    for($i=0;$i<$cnt;$i++)
    {
        $metal_name = $silver_and_pgm[$i][0];
        $bid = $silver_and_pgm[$i][1];
        $change = $silver_and_pgm[$i][2];

        //echo "here";
        echo $metal_name."<br>";
        echo $bid."<br>";
        echo $change."<br>";


        //$query = "insert into 'silver_and_pgm' values('$metal_name','$bid','$change')";
        //$query = "UPDATE silver_and_pgm set _bid='$bid' WHERE _metal_name='$metal_name'";

        //$query = "UPDATE silver_and_pgm set _bid = '$bid' WHERE _metal_name = '$metal_name'";
        $query = "UPDATE silver_and_pgm set _bid='$bid',_change = '$change' WHERE _metal_name 
                    like'$metal_name%'";


        echo $query."<br>";
        $result = mysql_query($query);
        if(!$result)
            echo "problem in silver_and_pgm"."<br>";

    }




}



?>

它在我的localhost中运行正常。但不是在我的网络服务器上。它抓取数据并将它们存储在localhost中...没有查询,没有echo语句在Web服务器中工作。 有人请建议一种方法来处理这个问题??

1 个答案:

答案 0 :(得分:0)

DOM需要PHP 5(并且要安装libxml)。通过运行php_info()确保您的服务器满足要求。此外,请返回错误报告并查看您获得的错误。