熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.Net獲得新浪天氣預報幾種方式總結

2013-11-13 10:23:43  來源: .NET編程 

  剛趕工寫了一個獲取新浪天氣預報的功能順便把代碼分享了出來以後再有這樣的工作大家可以直接把代碼拿來使用
 

  利用新浪提供給的iframe直接嵌入這種方式非常的簡單但是卻沒有交互性代碼如下

  <iframe frameborder= src= scrolling=no width= height=></iframe
抓取當天的天氣以指定格式輸出

  涉及的核心代碼如下

  Code
public static ArrayList GetWeather(string code)
        {
            /*
            [] 北京 string
            [] 雷陣雨 string
            [] string
            [] string
            [] 小於string
            */
            string html = ;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequestCreate( + code + l );
                requestMethod = Get;
                //requestTimeout   =   ;
                requestContentType = application/xwwwformurlencoded ;
                WebResponse response = requestGetResponse();
                Stream s = responseGetResponseStream();
                StreamReader sr = new StreamReader(s SystemTextEncodingGetEncoding(GB));
                html = srReadToEnd();
                sClose();
                srClose();
            }
            catch (Exception err)
            {
                throw new Exception(訪問地址出錯~~~ );
            }

  int count = htmlLength;
            int starIndex = htmlIndexOf(<table count);
            int endIndex = htmlIndexOf(</table> starIndex count starIndex);
            html = htmlSubstring(starIndex endIndex starIndex + );

  //得到城市
            int cityStartIndex = htmlIndexOf(<b> htmlLength);
            int cityEndIndex = htmlIndexOf(</b> htmlLength);
            string City = htmlSubstring(cityStartIndex + cityEndIndex cityStartIndex );

  //得到天氣
            int weatherStartIndex = htmlIndexOf(<b> cityEndIndex);
            int weatherEndIndex = htmlIndexOf(</b> weatherStartIndex);
            string Weather = htmlSubstring(weatherStartIndex + weatherEndIndex weatherStartIndex );

  //得到溫度

  int temperatureStartIndex = htmlIndexOf(<b weatherEndIndex);
            int temperatureEndIndex = htmlIndexOf(</b> weatherEndIndex + );
            string Temperature = htmlSubstring(temperatureStartIndex + temperatureEndIndex temperatureStartIndex );

  int int = TemperatureIndexOf( );
            int int = TemperatureIndexOf( );
            int int = TemperatureIndexOf( int);

  string MinTemperature = TemperatureSubstring(int + int int);
            string MaxTemperature = TemperatureSubstring( int int + );

  //得到風力
            int windforceStartIndex = htmlIndexOf(風力 temperatureEndIndex);
            int windforceEndIndex = htmlIndexOf(<br> windforceStartIndex);
            string Windforce = htmlSubstring(windforceStartIndex + windforceEndIndex windforceStartIndex );

  if (WindforceContains(小於) && (!WindforceContains(等於)))                  //判斷風力是否含有小於小於等於字樣將如果有的話將其替換為
            {
                //Windforce = WindforceReplace(小於 );
                string strWindforce = WindforceSubstring( WindforceLength );
                int minWindforce = IntParse(strWindforce) ;
                Windforce = WindforceReplace(小於 minWindforceToString() + );

  }
            else if (WindforceContains(小於等於))
            {
                string strWindforce = WindforceSubstring( WindforceLength );
                int minWindforce = IntParse(strWindforce) ;
                Windforce = WindforceReplace(小於等於 minWindforceToString() + );
            }

  ArrayList al = new ArrayList();
            alAdd(City);
            alAdd(Weather);
            alAdd(MinTemperature);
            alAdd(MaxTemperature);
            alAdd(Windforce);

  return al;
        }
 這裡涉及到一個ConvertCode類它的作用是用於把城市轉換為對應的全國統一的編碼

  代碼如下

  Code
using System;
using SystemCollectionsGeneric;
using SystemWeb;

  namespace WebApplication
{
    public class ConvertCode
    {
        public static string ConvertCodeByCity(string City)
        {
            string Code = ;
            switch (City)
            {
                case 北京:
                    Code = ;
                    break;
                case 天津:
                    Code = ;
                    break;
                case 石家莊:
                    Code = ;
                    break;
                case 唐山:
                    Code = ;
                    break;
                case 秦皇島:
                    Code = ;
                    break;
                case 張家口:
                    Code = ;
                    break;
                case 承德:
                    Code = ;
                    break;
                case 廊坊:
                    Code = ;
                    break;
                case 邯鄲:
                    Code = ;
                    break;
                case 邢台:
                    Code = ;
                    break;
                case 保定:
                    Code = ;
                    break;
                case 滄州:
                    Code = ;
                    break;
                case 衡水:
                    Code = ;
                    break;
                case 太原:
                    Code = ;
                    break;
                case 大同:
                    Code = ;
                    break;
                case 陽泉:
                    Code = ;
                    break;
                case 晉城:
                    Code = ;
                    break;
                case 朔州:
                    Code = ;
                    break;
                case 忻州:
                    Code = ;
                    break;
                case 離石:
                    Code = ;
                    break;
                case 榆次:
                    Code = ;
                    break;
                case 臨汾:
                    Code = ;
                    break;
                case 運城:
                    Code = ;
                    break;
                case 長治:
                    Code = ;
                    break;
                case 呼和浩特:
                    Code = ;
                    break;
                case 包頭:
                    Code = ;
                    break;
                case 烏海:
                    Code = ;
                    break;
                case 集寧:
                    Code = ;
                    break;
                case 巴彥浩特:                           //這個城市搜索不到
                    Code = ;
                    break;
                case 臨河:
                    Code = ;
                    break;
                case 鄂爾多斯:                           //這個城市搜索不到
                    Code = ;
                    break;
                case 赤峰:
                    Code = ;
                    break;
                case 通遼:
                    Code = ;
                    break;
                case 錫林浩特:
                    Code = ;
                    break;
                case 海拉爾:                            //這個城市搜索不到
                    Code = ;
                    break;
                case 烏蘭浩特:
                    Code = ;
                    break;
                case 沈陽:
                    Code = ;
                    break;
                case 大連:
                    Code = ;
                    break;
                case 鞍山:
                    Code = ;
                    break;
                case 撫順:
                    Code = ;
                    break;
                case 本溪:
                    Code = ;
                    break;
                case 錦州:
                    Code = ;
                    break;
                case 營口:
                    Code = ;
                    break;
                case 阜新:
                    Code = ;
                    break;
                case 盤錦:
                    Code = ;
                    break;
                case 鐵嶺:
                    Code = ;
                    break;
                case 朝陽:
                    Code = ;
                    break;
                case 葫蘆島:
                    Code = ;
                    break;
                case 丹東:
                    Code = ;
                    break;
                case 長春:
                    Code = ;
                    break;
                case 吉林:
                    Code = ;
                    break;
                case 四平:
                    Code = ;
                    break;
                case 遼源:
                    Code = ;
                    break;
                case 松原:
                    Code = ;
                    break;
                case 白城:
                    Code = ;
                    break;
                case 延邊:
                    Code = ;
                    break;
                case 通化:
                    Code = ;
                    break;
                case 哈爾濱:
                    Code = ;
                    break;
                case 雞西:
                    Code = ;
                    break;
                case 鶴崗:
                    Code = ;
                    break;
                case 雙鴨山:
                    Code = ;
                    break;
                case 伊春:
                    Code = ;
                    break;
                case 佳木斯:
                    Code = ;
                    break;
                case 七台河:
                    Code = ;
                    break;
                case 牡丹江:
                    Code = ;
                    break;
                case 綏化:
                    Code = ;
                    break;
                case 齊齊哈爾:
                    Code = ;
                    break;
                case 大慶:
                    Code = ;
                    break;
                case 黑河:
                    Code = ;
                    break;
                case 大興安嶺:
                    Code = ;
                    break;
                case 上海:
                    Code = ;
                    break;
                case 南京:
                    Code = ;
                    break;
                case 無錫:
                    Code = ;
                    break;
                case 徐州:
                    Code = ;
                    break;
                case 常州:
                    Code = ;
                    break;
                case 蘇州:
                    Code = ;
                    break;
                case 南通:
                    Code = ;
                    break;
                case 連雲港:
                    Code = ;
                    break;
                case 淮陰:
                    Code = ;
                    break;
                case 鹽城:
                    Code = ;
                    break;
                case 揚州:
                    Code = ;
                    break;
                case 鎮江:
                    Code = ;
                    break;
                case 泰州:
                    Code = ;
                    break;
                case 宿遷:
                    Code = ;
                    break;
                case 杭州:
                    Code = ;
                    break;
                case 寧波:
                    Code = ;
                    break;
                case 溫州:
                    Code = ;
                    break;
                case 嘉興:
                    Code = ;
                    break;
                case 湖州:
                    Code = ;
                    break;
                case 紹興:
                    Code = ;
                    break;
                case 金華:
                    Code = ;
                    break;
                case 衢州:
                    Code = ;
                    break;
                case 舟山:
                    Code = ;
                    break;
                case 麗水:
                    Code = ;
                    break;
                case 台州:
                    Code = ;
                    break;
                case 合肥:
                    Code = ;
                    break;
                case 蕪湖:
                    Code = ;
                    break;
                case 蚌埠:
                    Code = ;
                    break;
                case 淮南:
                    Code = ;
                    break;
                case 馬鞍山:
                    Code = ;
                    break;
                case 淮北:
                    Code = ;
                    break;
                case 銅陵:
                    Code = ;
                    break;
                case 安慶:
                    Code = ;
                    break;
                case 黃山市:
                    Code = ;
                    break;
                case 阜陽:
                    Code = ;
                    break;
                case 宿州:
                    Code = ;
                    break;
                case 滁州:
                    Code = ;
                    break;
                case 六安:
                    Code = ;
                    break;
                case 宣城:
                    Code = ;
                    break;
                case 巢湖:
                    Code = ;
                    break;
                case 池州:
                    Code = ;
                    break;
                case 福州:
                    Code = ;
                    break;
                case 廈門:
                    Code = ;
                    break;
                case 莆田:
                    Code = ;
                    break;
                case 三明:
                    Code = ;
                    break;
                case 泉州:
                    Code = ;
                    break;
                case 漳州:
                    Code = ;
                    break;
                case 南平:
                    Code = ;
                    break;
                case 寧德:
                    Code = ;
                    break;
                case 龍巖:
                    Code = ;
                    break;
                case 隴南:
                    Code = ;
                    break;
                case 慶陽:
                    Code = ;
                    break;
                case 南昌:
                    Code = ;
                    break;
                case 景德鎮:
                    Code = ;
                    break;
                case 贛州:
                    Code = ;
                    break;
                case 萍鄉:
                    Code = ;
                    break;
                case 九江:
                    Code = ;
                    break;
                case 新余:
                    Code = ;
                    break;
                case 鷹潭:
                    Code = ;
                    break;
                case 宜春:
                    Code = ;
                    break;
                case 上饒:
                    Code = ;
                    break;
                case 吉安:
                    Code = ;
                    break;
                case 濟南:
                    Code = ;
                    break;
                case 青島:
                    Code = ;
                    break;
                case 淄博:
                    Code = ;
                    break;
                case 棗莊:
                    Code = ;
                    break;
                case 東營:
                    Code = ;
                    break;
                case 煙台:
                    Code = ;
                    break;
                case 濰坊:
                    Code = ;
                    break;
                case 濟寧:
                    Code = ;
                    break;
                case 泰安:
                    Code = ;
                    break;
                case 威海:
                    Code = ;
                    break;
                case 日照:
                    Code = ;
                    break;
                case 濱州:
                    Code = ;
                    break;
                case 德州:
                    Code = ;
                    break;
                case 聊城:
                    Code = ;
                    break;
                case 臨沂:
                    Code = ;
                    break;
                case 菏澤:
                    Code = ;
                    break;
                case 萊蕪:
                    Code = ;
                    break;
                case 鄭州:
                    Code = ;
                    break;
                case 開封:
                    Code = ;
                    break;
                case 洛陽:
                    Code = ;
                    break;
                case 平頂山:
                    Code = ;
                    break;
                case 安陽:
                    Code = ;
                    break;
                case 鶴壁:
                    Code = ;
                    break;
                case 新鄉:
                    Code = ;
                    break;
                case 焦作:
                    Code = ;
                    break;
                case 濮陽:
                    Code = ;
                    break;
                case 許昌:
                    Code = ;
                    break;
                case 漯河:
                    Code = ;
                    break;
                case 三門峽:
                    Code = ;
                    break;
                case 商丘:
                    Code = ;
                    break;
                case 周口:
                    Code = ;
                    break;
                case 駐馬店:
                    Code = ;
                    break;
                case 南陽:
                    Code = ;
                    break;
                case 信陽:
                    Code = ;
                    break;
                case 武漢:
                    Code = ;
                    break;
                case 黃石:
                    Code = ;
                    break;
                case 十堰:
                    Code = ;
                    break;
                case 隨州:
                    Code = ;
                    break;
                case 宜昌:
                    Code = ;
                    break;
                case 襄樊:
                    Code = ;
                    break;
                case 鄂州:
                    Code = ;
                    break;
                case 荊門:
                    Code = ;
                    break;
                case 黃岡:
                    Code = ;
                    break;
                case 孝感:
                    Code = ;
                    break;
                case 鹹寧:
                    Code = ;
                    break;
                case 荊州:
                    Code = ;
                    break;
                case 恩施:
                    Code = ;
                    break;
                case 長沙:
                    Code = ;
                    break;
                case 衡陽:
                    Code = ;
                    break;
                case 邵陽:
                    Code = ;
                    break;
                case 郴州:
                    Code = ;
                    break;
                case 永州:
                    Code = ;
                    break;
                case 韶山:
                    Code = ;
                    break;
                case 張家界:
                    Code = ;
                    break;
                case 懷化:
                    Code = ;
                    break;
                case 吉首:
                    Code = ;
                    break;
                case 株洲:
                    Code = ;
                    break;
                case 湘潭:
                    Code = ;
                    break;
                case 岳陽:
                    Code = ;
                    break;
                case 常德:
                    Code = ;
                    break;
                case 益陽:
                    Code = ;
                    break;
                case 婁底:
                    Code = ;
                    break;
                case 廣州:
                    Code = ;
                    break;
                case 佛山:
                    Code = ;
                    break;
                case 深圳:
                    Code = ;
                    break;
                case 汕尾:
                    Code = ;
                    break;
                case 惠州:
                    Code = ;
                    break;
                case 河源:
                    Code = ;
                    break;
                case 清遠:
                    Code = ;
                    break;
                case 東莞:
                    Code = ;
                    break;
                case 珠海:
                    Code = ;
                    break;
                case 江門:
                    Code = ;
                    break;
                case 肇慶:
                    Code = ;
                    break;
                case 中山:
                    Code = ;
                    break;
                case 湛江:
                    Code = ;
                    break;
                case 茂名:
                    Code = ;
                    break;
                case 韶關:
                    Code = ;
                    break;
                case 汕頭:
                    Code = ;
                    break;
                case 梅州:
                    Code = ;
                    break;
                case 陽江:
                    Code = ;
                    break;
                case 潮州:
                    Code = ;
                    break;
                case 順德:
                    Code = ;
                    break;
                case 揭陽:
                    Code = ;
                    break;
                case 雲浮:
                    Code = ;
                    break;
                case 南寧:
                    Code = ;
                    break;
                case 梧州:
                    Code = ;
                    break;
                case 玉林:
                    Code = ;
                    break;
                case 桂林:
                    Code = ;
                    break;
                case 百色:
                    Code = ;
                    break;
                case 河池:
                    Code = ;
                    break;
                case 欽州:
                    Code = ;
                    break;
                case 柳州:
                    Code = ;
                    break;
                case 北海:
                    Code = ;
                    break;
                case 防城港:
                    Code = ;
                    break;
                case 貴港:
                    Code = ;
                    break;
                case 賀州:
                    Code = ;
                    break;
                case 海口:
                    Code = ;
                    break;
                case 三亞:
                    Code = ;
                    break;
                case 西沙群島:
                    Code = ;
                    break;
                case 成都:
                    Code = ;
                    break;
                case 眉山:
                    Code = ;
                    break;
                case 雅安:
                    Code = ;
                    break;
                case 峨嵋山:
                    Code = ;
                    break;
                case 自貢:
                    Code = ;
                    break;
                case 重慶:
                    Code = ;
                    break;
                case 萬州:
                    Code = ;
                    break;
                case 涪陵:
                    Code = ;
                    break;
                case 南充:
                    Code = ;
                    break;
                case 泸州:
                    Code = ;
                    break;
                case 德陽:
                    Code = ;
                    break;
                case 綿陽:
                    Code = ;
                    break;
                case 遂寧:
                    Code = ;
                    break;
                case 內江:
                    Code = ;
                    break;
                case 樂山:
                    Code = ;
                    break;
                case 宜賓:
                    Code = ;
                    break;
                case 廣元:
                    Code = ;
                    break;
                case 達州:
                    Code = ;
                    break;
                case 資陽:
                    Code = ;
                    break;
                case 攀枝花:
                    Code = ;
                    break;
                case 阿壩:
                    Code = ;
                    break;
                case 甘孜:
                    Code = ;
                    break;
                case 涼山:
                    Code = ;
                    break;
                case 廣安:
                    Code = ;
                    break;
                case 巴中:
                    Code = ;
                    break;
                case 黔江:
                    Code = ;
                    break;
                case 貴陽:
                    Code = ;
                    break;
                case 六盤水:
                    Code = ;
                    break;
                case 銅仁:
                    Code = ;
                    break;
                case 安順:
                    Code = ;
                    break;
                case 凱裡:
                    Code = ;
                    break;
                case 都勻:
                    Code = ;
                    break;
                case 興義:
                    Code = ;
                    break;
                case 畢節:
                    Code = ;
                    break;
                case 遵義:
                    Code = ;
                    break;
                case 昆明:
                    Code = ;
                    break;
                case 德宏:
                    Code = ;
                    break;
                case 曲靖:
                    Code = ;
                    break;
                case 楚雄:
                    Code = ;
                    break;
                case 玉溪:
                    Code = ;
                    break;
                case 紅河:
                    Code = ;
                    break;
                case 文山:
                    Code = ;
                    break;
                case 思茅:
                    Code = ;
                    break;
                case 昭通:
                    Code = ;
                    break;
                case 西雙版納:
                    Code = ;
                    break;
                case 大理:
                    Code = ;
                    break;
                case 保山:
                    Code = ;
                    break;
                case 怒江:
                    Code = ;
                    break;
                case 麗江:
                    Code = ;
                    break;
                case 迪慶:
                    Code = ;
                    break;
                case 臨滄:
                    Code = ;
                    break;
                case 拉薩:
                    Code = ;
                    break;
                case 昌都:
                    Code = ;
                    break;
                case 山南:
                    Code = ;
                    break;
                case 日喀則:
                    Code = ;
                    break;
                case 那曲:
                    Code = ;
                    break;
                case 阿裡:
                    Code = ;
                    break;
                case 林芝:
                    Code = ;
                    break;
                case 西安:
                    Code = ;
                    break;
                case 銅川:
                    Code = ;
                    break;
                case 寶雞:
                    Code = ;
                    break;
                case 鹹陽:
                    Code = ;
                    break;
                case 渭南:
                    Code = ;
                    break;
                case 漢中:
                    Code = ;
                    break;
                case 安康:
                    Code = ;
                    break;
                case 商洛:
                    Code = ;
                    break;
                case 延安:
                    Code = ;
                    break;
                case 榆林:
                    Code = ;
                    break;
                case 蘭州:
                    Code = ;
                    break;
                case 白銀:
                    Code = ;
                    break;
                case 金昌:
                    Code = ;
                    break;
                case 天水:
                    Code = ;
                    break;
                case 張掖:
                    Code = ;
                    break;
                case 武威:
                    Code = ;
                    break;
                case 定西:
                    Code = ;
                    break;
                case 平涼:
                    Code = ;
                    break;
                case 臨夏:
                    Code = ;
                    break;
                case 嘉峪關:
                    Code = ;
                    break;
                case 酒泉:
                    Code = ;
                    break;
                case 西寧:
                    Code = ;
                    break;
                case 果洛:
                    Code = ;
                    break;
                case 海西:
                    Code = ;
                    break;
                case 格爾木:
                    Code = ;
                    break;
                case 海東:
                    Code = ;
                    break;
                case 海北:
                    Code = ;
                    break;
                case 玉樹:
                    Code = ;
                    break;
                case 黃南:
                    Code = ;
                    break;
                case 銀川:
                    Code = ;
                    break;
                case 石嘴山:
                    Code = ;
                    break;
                case 吳忠:
                    Code = ;
                    break;
                case 固原:
                    Code = ;
                    break;
                case 烏魯木齊:
                    Code = ;
                    break;
                case 克拉瑪依:
                    Code = ;
                    break;
                case 吐魯番:
                    Code = ;
                    break;
                case 哈密:
                    Code = ;
                    break;
                case 昌吉:
                    Code = ;
                    break;
                case 博樂:
                    Code = ;
                    break;
                case 庫爾勒:
                    Code = ;
                    break;
                case 阿克蘇:
                    Code = ;
                    break;
                case 克州:
                    Code = ;
                    break;
                case 喀什:
                    Code = ;
                    break;
                case 伊犁:
                    Code = ;
                    break;
                case 石河子:
                    Code = ;
                    break;
                case 塔城:
                    Code = ;
                    break;
                case 阿勒泰:
                    Code = ;
                    break;
                case 台北:
                    Code = ;
                    break;
                case 遼陽:
                    Code = ;
                    break;
                case 和田:
                    Code = ;
                    break;
                case 澳門:
                    Code = ;
                    break;
                case 香港:
                    Code = ;
                    break;
                   
                default:
                    break;
            }
            return Code;
        }
    }
}

  獲取三天以內的天氣以指定格式輸出

  核心代碼如下

  Code
public static ArrayList GetThreeDayWeather(string City)
        {
            ArrayList al = new ArrayList();
            /*
           [] 今天 北京              string
           [] 星期五     string
           [] 晴轉多雲               string
           []                    string
           []                    string
           []                   string
           [] 明天 北京              string
           [] 星期六     string
           [] 陰轉陣雨               string
           []                    string
           []                   string
           []                  string
           [] 後天 北京             string
           [] 星期日    string
           [] 多雲轉陣雨            string
           []                    string
           []                   string
           []                  string        
           */
            string Html = ;       //返回來的網頁的源碼
            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = stringFormat(city= + City);
            byte[] data = encodingGetBytes(postData);

  try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequestCreate(?city= + SystemWebHttpContextCurrentServerUrlEncode(City) + &f=&dpc=);
                requestMethod = Get;
                requestContentType = application/xwwwformurlencoded ;
                WebResponse response = requestGetResponse();
                Stream s = responseGetResponseStream();
                StreamReader sr = new StreamReader(s SystemTextEncodingGetEncoding(GB));
                Html = srReadToEnd();
                sClose();
                srClose();
            }
            catch (Exception err)
            {
                throw new Exception(訪問地址出錯~~~ );
            }

  //去除多余代碼便於分析跟提高效率
            int count = HtmlLength;
            int starIndex = HtmlIndexOf(<div id=\WeatherDBlk\> count);
            int endIndex = HtmlIndexOf(<div id=\SideBar\> );
            Html = HtmlSubstring(starIndex endIndex starIndex);

  try
            {
                #region 得到今天的天氣

  //得到今天的標識跟城市
                int firstDayAndCityStartIndex = HtmlIndexOf(<h> );
                int firstDayAndCityEndIndex = HtmlIndexOf(</h> );
                string FirstDayAndCity = HtmlSubstring(firstDayAndCityStartIndex + firstDayAndCityEndIndex firstDayAndCityStartIndex );

  //得到今天的日期跟星期
                int firstDateStartIndex = HtmlIndexOf(<p> firstDayAndCityEndIndex);
                int firstDateEndIndex = HtmlIndexOf(</p> firstDayAndCityEndIndex);
                string FirstDate = HtmlSubstring(firstDateStartIndex + firstDateEndIndex firstDateStartIndex )Replace(&nbsp; );

  //得到今天的天氣
                int firstWeatherStartIndex = HtmlIndexOf(<div class=\Weather_TP\> firstDateEndIndex);
                int firstWeatherEndIndex = HtmlIndexOf( firstWeatherStartIndex + );
                string FirstWeather = HtmlSubstring(firstWeatherStartIndex + firstWeatherEndIndex firstWeatherStartIndex );

  //得到今天的溫度

  int firstTemperatureStartIndex = firstWeatherEndIndex + ;
                int firstTemperatureEndIndex = HtmlIndexOf(</div> firstTemperatureStartIndex);
                string FirstTemperature = HtmlSubstring(firstTemperatureStartIndex firstTemperatureEndIndex firstTemperatureStartIndex);
                int int = FirstTemperatureIndexOf( );
                int int = FirstTemperatureIndexOf( );
                int int = FirstTemperatureIndexOf( int);
                string FirstMinTemperature = FirstTemperatureSubstring(int + int int);
                string FirstMaxTemperature = FirstTemperatureSubstring( int int + );

  //得到今天的風力
                int firstWindforceStartIndex = HtmlIndexOf(風力 firstTemperatureEndIndex);
                int firstWindforceEndIndex = HtmlIndexOf(</div> firstWindforceStartIndex);
                string FirstWindforce = HtmlSubstring(firstWindforceStartIndex + firstWindforceEndIndex firstWindforceStartIndex );

  if (FirstWindforceContains())
                {

  }
                else if (FirstWindforceContains(<))                  //判斷風力是否含有<字樣將如果有的話將其替換為
                {
                    string strWindforce = FirstWindforceSubstring( FirstWindforceLength );
                    int minWindforce = IntParse(strWindforce) ;
                    FirstWindforce = FirstWindforceReplace(< minWindforceToString() + );
                }
                else if (FirstWindforceContains())
                {
                    string strWindforce = FirstWindforceSubstring( FirstWindforceLength );
                    int minWindforce = IntParse(strWindforce) ;
                    FirstWindforce = FirstWindforceReplace( minWindforceToString() + );
                }

  #endregion

  #region 得到明天的天氣

  //得到明天的標識跟城市
                int secondDayAndCityStartIndex = HtmlIndexOf(<h> firstWindforceEndIndex);
                int secondDayAndCityEndIndex = HtmlIndexOf(</h> secondDayAndCityStartIndex);
                string secondDayAndCity = HtmlSubstring(secondDayAndCityStartIndex + secondDayAndCityEndIndex secondDayAndCityStartIndex );

  //得到明天的日期跟星期
                int secondDateStartIndex = HtmlIndexOf(<p> secondDayAndCityEndIndex);
                int secondDateEndIndex = HtmlIndexOf(</p> secondDateStartIndex);
                string SecondDate = HtmlSubstring(secondDateStartIndex + secondDateEndIndex secondDateStartIndex )Replace(&nbsp; );

  //得到明天的天氣
                int secondWeatherStartIndex = HtmlIndexOf(<div class=\Weather_TP\> secondDateEndIndex);
                int secondWeatherEndIndex = HtmlIndexOf( secondWeatherStartIndex + );
                string SecondWeather = HtmlSubstring(secondWeatherStartIndex + secondWeatherEndIndex secondWeatherStartIndex );

  //得到明天的溫度

  int secondTemperatureStartIndex = secondWeatherEndIndex + ;
                int secondTemperatureEndIndex = HtmlIndexOf(</div> secondTemperatureStartIndex);
                string SecondTemperature = HtmlSubstring(secondTemperatureStartIndex secondTemperatureEndIndex secondTemperatureStartIndex);
                int int = SecondTemperatureIndexOf( );
                int int = SecondTemperatureIndexOf( );
                int int = SecondTemperatureIndexOf( int);
                string SecondMinTemperature = SecondTemperatureSubstring(int + int int);
                string SecondMaxTemperature = SecondTemperatureSubstring( int int + );

  //得到明天的風力
                int secondWindforceStartIndex = HtmlIndexOf(風力 secondTemperatureEndIndex);
                int secondWindforceEndIndex = HtmlIndexOf(</div> secondWindforceStartIndex);
                string SecondWindforce = HtmlSubstring(secondWindforceStartIndex + secondWindforceEndIndex secondWindforceStartIndex );

  if (SecondWindforceContains())
                {

  }
                else if (SecondWindforceContains(<))                  //判斷風力是否含有<字樣將如果有的話將其替換為
                {
                    string strWindforce = SecondWindforceSubstring( FirstWindforceLength );
                    int minWindforce = IntParse(strWindforce) ;
                    SecondWindforce = SecondWindforceReplace(< minWindforceToString() + );
                }
                else if (SecondWindforceContains())
                {
                    string strWindforce = SecondWindforceSubstring( SecondWindforceLength );
                    int minWindforce = IntParse(strWindforce) ;
                    SecondWindforce = SecondWindforceReplace( minWindforceToString() + );
                }

  #endregion

  #region 得到後天的天氣

  //得到後天的標識跟城市
                int thirdDayAndCityStartIndex = HtmlIndexOf(<h> secondWindforceEndIndex);
                int thirdDayAndCityEndIndex = HtmlIndexOf(</h> thirdDayAndCityStartIndex);
                string thirdDayAndCity = HtmlSubstring(thirdDayAndCityStartIndex + thirdDayAndCityEndIndex thirdDayAndCityStartIndex );

  //得到後天的日期跟星期
                int thirdDateStartIndex = HtmlIndexOf(<p> thirdDayAndCityEndIndex);
                int thirdDateEndIndex = HtmlIndexOf(</p> thirdDateStartIndex);
                string ThirdDate = HtmlSubstring(thirdDateStartIndex + thirdDateEndIndex thirdDateStartIndex )Replace(&nbsp; );

  //得到後天的天氣
                int thirdWeatherStartIndex = HtmlIndexOf(<div class=\Weather_TP\> thirdDateEndIndex);
                int thirdWeatherEndIndex = HtmlIndexOf( thirdWeatherStartIndex + );
                string ThirdWeather = HtmlSubstring(thirdWeatherStartIndex + thirdWeatherEndIndex thirdWeatherStartIndex );

  //得到後天的溫度

  int thirdTemperatureStartIndex = thirdWeatherEndIndex + ;
                int thirdTemperatureEndIndex = HtmlIndexOf(</div> thirdTemperatureStartIndex);
                string ThirdTemperature = HtmlSubstring(thirdTemperatureStartIndex thirdTemperatureEndIndex thirdTemperatureStartIndex);
                int int = ThirdTemperatureIndexOf( );
                int int = ThirdTemperatureIndexOf( );
                int int = ThirdTemperatureIndexOf( int);
                string ThirdMinTemperature = ThirdTemperatureSubstring(int + int int);
                string ThirdMaxTemperature = ThirdTemperatureSubstring( int int + );

  //得到後天的風力
                int thirdWindforceStartIndex = HtmlIndexOf(風力 thirdTemperatureEndIndex);
                int thirdWindforceEndIndex = HtmlIndexOf(</div> thirdWindforceStartIndex);
                string ThirdWindforce = HtmlSubstring(thirdWindforceStartIndex + thirdWindforceEndIndex thirdWindforceStartIndex );

  if (ThirdWindforceContains())
                {

  }
                else if (ThirdWindforceContains(<))                  //判斷風力是否含有<字樣將如果有的話將其替換為
                {
                    string strWindforce = ThirdWindforceSubstring( ThirdWindforceLength );
                    int minWindforce = IntParse(strWindforce) ;
                    ThirdWindforce = ThirdWindforceReplace(< minWindforceToString() + );
                }
                else if (ThirdWindforceContains())
                {
                    string strWindforce = ThirdWindforceSubstring( ThirdWindforceLength );
                    int minWindforce = IntParse(strWindforce) ;
                    ThirdWindforce = ThirdWindforceReplace( minWindforceToString() + );
                }

  #endregion

  alAdd(FirstDayAndCity);
                alAdd(FirstDate);
                alAdd(FirstWeather);
                alAdd(FirstMinTemperature);
                alAdd(FirstMaxTemperature);
                alAdd(FirstWindforce);

  alAdd(secondDayAndCity);
                alAdd(SecondDate);
                alAdd(SecondWeather);
                alAdd(SecondMinTemperature);
                alAdd(SecondMaxTemperature);
                alAdd(SecondWindforce);

  alAdd(thirdDayAndCity);
                alAdd(ThirdDate);
                alAdd(ThirdWeather);
                alAdd(ThirdMinTemperature);
                alAdd(ThirdMaxTemperature);
                alAdd(ThirdWindforce);
            }
            catch (Exception err)
            {

  }

  return al;
        } 
 
特殊說明使用第三種方法獲取天氣預報輸入城市的時候可能會受新浪提供的服務的影響可能有些城市搜不到前兩種方法應該是不會受影響的另外由於代碼寫的比較急所以難免代碼的質量就會有些問題還請大家多多包涵單純從代碼上看可能確實沒有什麼難度可是如果在您的工作中如果因為我的代碼為您節省了一些時間筆者深感欣慰了
 

  另外由於我的開發環境是vs+sp如果您的vs版本較低不妨把項目文件刪除掉然後打開您的vs選擇打開網站然後定位到本程序的目錄這樣就可以進行代碼的編譯跟蹤調試了
 

  再補充一點有人可能對代碼中出現的SystemEnvironmentNewLine 這句代碼比較迷糊因為這個提供的簡單的換行方法我推薦大家以後能夠使用這提供給大家的高效簡便的方法最關鍵的是它不容易出錯


From:http://tw.wingwit.com/Article/program/net/201311/13478.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.