• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 47, 文章 - 10, 評論 - 8, 引用 - 0
            數據加載中……

            抓取騰訊天氣預報的類

            類的代碼:

              1<?php
              2/**
              3 * 抓取騰訊天氣,存儲成XML
              4 * @author PeterPan
              5 * @final 2007-08-27
              6 */
              7class Weather{
              8    /**成員變量*/
              9    var $m_city_list = array();
             10    var $m_data = array();
             11    var $m_url_tpl = "http://weather.news.qq.com/inc/07_ss{city_code}.htm";
             12    var $m_url = "";
             13    var $m_city_code = "125";
             14    /**
             15     * 構造函數
             16     */
             17    function __construct($city_code=""){
             18        if ($city_code){
             19            $this->m_city_code = $m_city_code;
             20        }
             21    }
             22    function __destruct(){
             23        unset($this->m_data);
             24        unset($this->m_url_tpl);
             25        unset($this->m_url);
             26        unset($this->m_city_code);
             27    }
             28    /**
             29     * 根據$url提供的地址,讀取數據
             30     *
             31     * @param unknown_type $url
             32     */
             33    function getUrlContent($url){
             34        //先嘗試用file_get_contents獲取
             35        if ( $data=file_get_contents($url) ){
             36            return $data;
             37        }
             38        else {
             39            $url_a = parse_url($url);
             40            if ( $url_a['scheme']!="http"&&$url_a['scheme']!="https" ){
             41                return "";
             42            }
             43            if ( empty($url_a['port']) ){
             44                $url_a['port'= 80;
             45            }
             46            //開始連接
             47            $data = "";
             48            if ( $fp=fsockopen($url_a['host'],$url_a['port'],$errno,$errstr,30) ){
             49                $request = "GET ".$url_a['path']." HTTP/1.1\r\n";
             50                $request .= "Host: ".$url_a['host']."\r\nConnection: Close\r\n\r\n";
             51                fwrite($fp,$request);
             52                $line = "";
             53                while ( $line=fread($fp,2048) ){
             54                    $data .= $line;
             55                }
             56                fclose($fp);
             57            }
             58            $pos = strpos($data,'<html>');
             59            $data = substr($data,$pos);
             60            echo $data;
             61            return $data;
             62        }
             63    }
             64    /**
             65     * 獲取某個城市的天氣情況,需要打開allow_url_open
             66     *
             67     * @param string    $city_code        城市代碼,默認為$m_city_code指定的城市
             68     * @param int        $reget            是否重新獲取,默認為0
             69     * @return array
             70     */
             71    function getWeather($city_code="",$reget=0){
             72        if ($city_code){
             73            $this->m_city_code = $city_code;
             74        }
             75        if ( $reget&&key_exists($this->m_city_code,$this->m_data) ){
             76            return $this->m_data[$this->m_city_code];
             77        }
             78        $this->m_url = str_replace("{city_code}",$this->m_city_code,$this->m_url_tpl);
             79        if ( $content = $this->getUrlContent($this->m_url) ){
             80            $content = iconv('gb2312','UTF-8',$content);
             81            $weather = array();
             82            //讀取城市信息
             83            $pattern = '/<td width="151" height="82" class="wht1 lk37">(\S+)<\/td>/ims';
             84            preg_match_all($pattern,$content,$matches);
             85            $weather['city'= trim($matches[1][0]);
             86            //讀取天氣情況
             87            $pattern = '/<td height="77" class="wht2 lk37">.<div class="txbd">(\S+)<\/div>([^<]+)<\/td>/ims';
             88            preg_match_all($pattern,$content,$matches);
             89            $weather['desc'= trim($matches[1][0]);
             90            $weather['temperature'= trim($matches[2][0]);
             91            //獲取天氣詳細
             92            $pattern = '/<td class="whp3 lk37">(.+)<\/td>/ims';
             93            preg_match_all($pattern,$content,$matches);
             94            $weather['detail'= trim($matches[1][0]);
             95            $weather['detail'= preg_replace('/([\r\n])[\s]+/','',$weather['detail']);
             96            $this->m_data[$this->m_city_code] = $weather;
             97            unset($weather);
             98            return $this->m_data[$this->m_city_code];
             99        }
            100        else {
            101            return array();
            102        }
            103    }
            104    /**
            105     * 獲取頁面中的所有地區
            106     *
            107     * @return array
            108     */
            109    function getAllCity(){
            110        $url = "http://weather.news.qq.com/inc/07_ss125.htm";
            111        if ( $content = $this->getUrlContent($url) ){
            112            $content = iconv('gb2312','UTF-8',$content);
            113            $city_list = array();
            114            //獲取城市列表
            115            $pattern = '/<option (selected="selected" )?value="(\S+)">(\S+)<\/option>/ims';
            116            preg_match_all($pattern,$content,$matches);
            117            $c = count($matches[2]);
            118            for ($i=0;$i<$c;$i++){
            119                $city_list[$matches[2][$i]] = trim($matches[3][$i]);
            120            }
            121            $this->m_city_list = $city_list;
            122            return $this->m_city_list;
            123        }
            124        else {
            125            return array();
            126        }
            127    }
            128    /**
            129     * 獲得所有城市的天氣情況
            130     *
            131     * @return array
            132     */
            133    function getAllCityWeather(){
            134        if ( empty($this->m_city_list) ){
            135            $this->m_city_list = $this->getAllCity();
            136        }
            137        foreach ($this->m_city_list as $key=>$value){
            138            $this->getWeather($key);
            139        }
            140        return $this->m_data;
            141    }
            142    /**
            143     * 將天氣信息存儲到XML文件中
            144     *
            145     * @param string    $xml_path        文件路徑
            146     */
            147    function saveXML($xml_path=""){
            148        $xml = '<?xml version="1.0" encoding="UTF-8"?>
            149        <root>
            150        ';
            151        foreach ($this->m_data as $key=>$value){
            152            $xml .= '
            153            <city code="'.$key.'" name="'.$value['city'].'">
            154            <desc><![CDATA['.$value['desc'].']]></desc>
            155            <temperature><![CDATA['.$value['temperature'].']]></temperature>
            156            <detail><![CDATA['.$value['detail'].']]></detail>
            157            </city>';
            158        }
            159        $xml .= '</root>';
            160        if ( $xml_path ){
            161            //打開并寫入數據
            162            if ( $fp=fopen($xml_path,"w") ){
            163                fwrite($fp,$xml);
            164            }
            165            fclose($fp);
            166        }
            167        return $xml;
            168    }
            169    /**
            170     * 從XML中讀取數據
            171     *
            172     * @param unknown_type $xml_path
            173     */
            174    function loadDataFromXML($xml_path){
            175        $this->m_city_list = array();
            176        $this->m_data = array();
            177        if ( $data=file_get_contents($xml_path) ){
            178            $xml = new SimpleXMLElement($data);
            179            foreach ($xml->city as $city){
            180                $weather = array('city'=>'','desc'=>'','temperature'=>'','detail'=>'');
            181                $code = (string)$city['code'];
            182                $name = (string)$city['name'];
            183                $this->m_city_list[$code= $name;
            184                $weather['city'= $name;
            185                $weather['desc'= (string)$city->desc;
            186                $weather['temperature'= (string)$city->temperature;
            187                $weather['detail'= (string)$city->detail;
            188                $this->m_data[$code= $weather;
            189            }
            190        }
            191        //var_dump($this->m_city_list);
            192        var_dump($this->m_data);
            193    }
            194}
            195?>

            測試程序:
             1<?php
             2header("Content-Type: text/html; charset=UTF-8");  //設置頁面編碼
             3$weather_obj = new Weather();
             4//獲取可用的城市
             5$city_list = $weather_obj->getAllCity();
             6var_dump($city_list);
             7echo "<br><br>";
             8//獲取北京的天氣情況,北京的代號是125
             9$result = $weather_obj->getWeather("125");
            10var_dump($result);
            11echo "<br><br>";
            12//獲取所有地區的天氣
            13$result = $weather_obj->getAllCityWeather();
            14var_dump($result);
            15echo "<br><br>";
            16//保存數據到文件
            17$xml = $weather_obj->saveXML("a.xml");
            18//從文件加載數據
            19$weather_obj->loadDataFromXML("a.xml");
            20var_dump($weather_obj->getAllCityWeather());
            21?>

            posted on 2007-08-27 16:37 編程之道 閱讀(2300) 評論(2)  編輯 收藏 引用 所屬分類: web編程開發相關

            評論

            # re: 抓取騰訊天氣預報的類  回復  更多評論   

            我也來學一招
            2007-12-21 12:02 | 秦歌

            # qlsupmbx  回復  更多評論   

            <a href="http://hkjuoywn.com">xzpycjut</a> [URL=http://cmoqvesh.com]sueeoqqn[/URL] rzrdylcc http://uzrbulpd.com jieaoxqh scbttszh
            2008-06-28 04:31 | qlsupmbx
            久久无码国产| 久久夜色精品国产噜噜亚洲AV| 成人资源影音先锋久久资源网| a级毛片无码兔费真人久久| 精品一二三区久久aaa片| 人妻少妇精品久久| 日韩欧美亚洲综合久久影院d3| 久久成人国产精品二三区| 狠狠色丁香久久婷婷综合| 国产成人综合久久综合| 久久人人爽人爽人人爽av | 奇米综合四色77777久久| 久久99国产精品一区二区| 久久精品极品盛宴观看| 97精品久久天干天天天按摩| 亚洲午夜精品久久久久久浪潮| 精品久久一区二区三区| 99久久国产精品免费一区二区| 久久综合久久性久99毛片| 一本大道加勒比久久综合| 精品久久久久久久久午夜福利| 99久久国产主播综合精品| 久久亚洲精品成人无码网站| 99国内精品久久久久久久| 国产精品国色综合久久| 99久久精品国内| 久久国产成人精品麻豆| 久久精品人妻一区二区三区| 四虎国产精品成人免费久久| AA级片免费看视频久久| 亚洲国产成人久久一区WWW| 欧美性大战久久久久久| 亚洲熟妇无码另类久久久 | 一本色道久久综合狠狠躁| 久久精品麻豆日日躁夜夜躁| 国产精品va久久久久久久| 久久性精品| 女同久久| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 久久国产精品无码HDAV| 91精品国产高清久久久久久91|