获取海洋天气预报

    protected string GetTq(out string publishDate)
    {
        
string getTxt = string.Empty;
        
try
        {
            
string url = "http://www.nmc.gov.cn/productframe/YB/HAIY_HYTQYB/1.html";
            HttpWebRequest request 
= (HttpWebRequest)WebRequest.Create(url);
            request.Referer 
= "http://www.nmc.gov.cn/product_forecast.php?v1=YB&v2=HAIY_HYTQYB&v3=1";
            request.Timeout 
= 5000;
            request.UserAgent 
= "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3";
            request.Accept 
= "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.Headers.Add(
"Accept-Encoding""gzip, deflate");
            request.Headers.Add(
"Accept-Language""zh-cn,zh;q=0.5");

            HttpWebResponse response 
= (HttpWebResponse)request.GetResponse();
            System.IO.StreamReader reader 
= null;// new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
            if (response.StatusCode == HttpStatusCode.OK)
            {
                
if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                    reader 
= new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), System.Text.Encoding.GetEncoding("gb2312"));
                
else
                    reader 
= new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
            }
            
string Result = reader.ReadToEnd().Replace("\"""").Replace(" """).Replace("\n""").Replace("\t""");
            //string Result = GetHtml(url);//.Replace("\"", "").Replace(" ", "").Replace("\n", "").Replace("\t", "");
            string regexStr = "<tdcolspan=2height=200>(?<key>.*?)</td>";
            Regex r 
= new Regex(regexStr, RegexOptions.None);
            Match mc 
= r.Match(Result);
            
string dataStr = mc.Groups["key"].Value;
            
//Response.Write(Result.Replace("<", "&lt;").Replace(">", "&gt") + "<hr/>");
            getTxt = dataStr.Replace("内&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;容:<br/><br/>""");
            
//发布时间
            string regpdate = "<tdheight=20>发布时间:(?<key>.*?)</td>";
            Regex rpd 
= new Regex(regpdate, RegexOptions.None);
            Match mcpd 
= rpd.Match(Result);
            publishDate 
= mcpd.Groups["key"].Value;

            reader.Close();
            response.Close();
        }
        
catch (Exception)
        {
            getTxt 
= string.Empty;
            publishDate 
= string.Empty;
        }

        
return getTxt;
    }

 

效果如下:
发布时间: 2009年2月11日10时
     中央气象台今天上午10时发布海上大风预报:
    2月11日上午8时,渤海、黄海大部海域出现了4-6级西北风;东海大部海域出现了4-5级西南风;台湾海峡、台湾以东洋面和以南海域、南海大部海域出现了4-6级东北风;北部湾出现了4-5级偏南风。渤海、黄海部分海域、东海西部海域、台湾海峡、北部湾有雾,能见度不足10公里。其他海域能见度及天气海况条件较好。
    预计,11日12时至12日12时,渤海、黄海大部海域、东海北部海域将有4-5级加大到7-8级东南风;东海南部海域将有4-5级加大到6-7级东南风;台湾以东洋面和以南海域、南海北部海域、北部湾将有4-6级东南或偏南风;南海中部和南部海域将有4-5级东北风。其他海域能见度及天气海况条件较好。
    12日12时至13日12时,渤海将有7-8级东南转东北风;黄海大部海域、东海大部海域将有7-8级、阵风9级的东南转西南风;台湾海峡将有6-7级、阵风8级西南风;台湾以东洋面将有5-7级东南到偏南风;台湾以南海域、南海大部海域、北部湾将有4-6级偏南风。其他海域能见度及天气海况条件较好。
    防御指南:
    中央气象台、中国海上搜救中心提醒受大风影响海域航行、作业的船舶,注意航行安全。

posted on 2009-02-11 11:06  myx  阅读(751)  评论(0编辑  收藏  举报