http://www.t7online.com/China.htm用fopen()获取html文件,然后分析html,提取自己要的信息

解决方案 »

  1.   

    哈哈,我做出回来了,每隔12小时,重新取一次数据,这样,会减轻网络负担的,<?php
    /*---- 城市天气预报
    根据http://www.t7online.com/cgi-bin/citybild?PROVIDER=anwendung&WMO=56294&LANG=cn
    网站的数据,从结果中进行分析,得到数据,并将数据存放于文件中,采用12小时生成一次,这样,不必每次都重新生成---*///要显示的城市
    $AryCityCode = array(
    "56294" => "成都", 
    "57206" => "广元", 
    "56196" => "绵阳", 
    "56198" => "德阳", 
    "56386" => "乐山", 
    "56391" => "眉山", 
    "57504" => "内江", 
    "56396" => "自贡", 
    "56491" => "宜宾", 
    "57415" => "广安");//每隔多长时间重新生成数据,小时数
    $option3 = "12";$file = "other/city_weather.cache"; //天气信息存放文件
    if(file_exists($file)) {
    //文件存在
    include_once($file);
    $ctime = time() - $option3 * 60 * 60;
    if ($ts > $ctime) {
    //所有城市,未超过更新时间
    $action = 1;
    $str    = $str;
    } else {
    $action = 0;
    }
    } else {
    $action = 0;
    }//调用函数,重新更新内容
    if($action == 0) {
         //设置系统超长时间
     set_time_limit(0);
     $fp = fopen($file, w);
     $content = "";
     //循环数据,重新生成所有城市的数据
     while (list($CityCode, $CityName) = each($AryCityCode)) { $content .= city_weather($CityCode);  }
     //生成时间标识,同时,写进数据文件中
     $str = "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"#FFFFFF\" bordercolor=\"#FFFFFF\">".$content."</table>";
     $content = "<?\$ts=\"".time()."\";\n\$str='".$str."';\n?>";  
     fwrite($fp, $content);
         fclose($fp);
    }echo $str; //显示天气预报数据//**********************************************
    //* 功能:根据城市代码,重新生成天气数据
    //* 来源:原创
    //* 参数(输入):option1--城市代码 option2--表格单元背景色
    //* 数据库操作:
    //* 输出:生成新的数据
    //* 时间:2003-11-21
    //* 作者:紫文科技 www.zwon.net 王友兴 (网名:海风习习 
    //* QQ:10196648 [email protected] [email protected]
    //**********************************************
    function city_weather($option1, $option2="#F2FDFB"){
    //from yahoo
    global $AryCityCode;//更新内容
    $UrlWeather = "http://www.t7online.com/cgi-bin/citybild?PROVIDER=anwendung&WMO=$option1&LANG=cn";
    $fp = fopen($UrlWeather, "r");$num = 1;  //行数变量初始为1,表示第一行while (!feof($fp)) {
    //如果大于80行,则退出循环,因此,以后的数据没有用
    if ($num >= 80) break;$msg = trim(fgets($fp, 4096)); //得到一行的数据//得到天气情况
    if ($num == 70) {
    eregi("alt=\".*\" width", $msg, $ary);
        $weather2 = str_replace("ALT=\"", "", $ary[0]);
    $weather2 = str_replace("\" width", "", $weather2);
    }//得到最低温度
    if ($num == 58) {
    eregi("<b>([0-9]+)&deg;C</b>", $msg, $ary);
    $weathermin = str_replace("&deg;C", "", $ary[0]);//替换掉其中的度数表示符号
    }//得到最高温度
    if ($num == 64) {
    eregi("<b>([0-9]+)&deg;C</b>", $msg, $ary);
    $weathermax =  $ary[0];
    }$num++; //行数变量加1
    }
    fclose($fp);$weather = $AryCityCode[$option1]." ".$weather2." 气温:".$weathermin."~".$weathermax;return "<tr><td bgcolor=\"$option2\">$weather</td></tr>\n";}
    ?>
      

  2.   

    56294" => "成都", 
    "57206" => "广元", 
    "56196" => "绵阳", 
    "56198" => "德阳", 
    "56386" => "乐山", 
    "56391" => "眉山", 
    "57504" => "内江", 
    "56396" => "自贡", 
    "56491" => "宜宾", 
    "57415" => "广安");这些城市代码是怎么得到的?如果我想取其它的城市应该用什么代替呢?
      

  3.   

    http://www.t7online.com/sichuang.htm,这上面有啊,你打开源文件,就可以看到了,