实战系列之天气预报实时采集

    xiaoxiao2022-07-09  169

    前言:

    今天 CYQ.Data 框架  框架群里,“路过冬天”问了个天气预报的问题,问哪里有webservice调用?于是随性就有了这篇文章了。

     

    天气预报,回忆中做过那么三次。

     

    第一次的做法是:

    技术总监写了个采集后台,每天早晚各采一次,从tq121站里采集大量的天气信息到数据库,我就直接从数据库里读数据了。

     

    总结:

    这种做法很麻烦,每天要开后台采数据,做成自动的,还要不半路程序自动死亡才行,而且数据库会产生大堆垃圾过时的数据。

    优点是:可以采集很多信息,做成很专业的天气预报站,那时候做旅游站,天气也是重要模块,所以这种方式也很合适。

     

    第二次:

    自己做毕业设计,都没采集后台,自己又写不出采集来,没数据读了,只好到处百度搜索“天气预报Webservice"调用。

     

    总结:

    这种做法也很郁闷,首先Webservice不好找,第二找到的如果小站提供的,随时又会挂掉了,要是人家挂掉,你要另找一个?

    优点是:找到就调用,什么也不用管,菜鸟也能飞。

     

    第三次:

    是电子商务平台在首页显示下天气,那时候正巧遇到刚做完web版的采集系统,于是顺理直接使用采集类库现采现显。

     

    总结:

    优点是:不用和数据库打交道,现采现显,减少数据库压力,速度快,每天只采一次,丁点信息,缓存即可。对于天气只是装饰性的极适用。

    缺点是:数据量少,不能做能专业性天气预报站。

     

    以下介绍现采现显的实现方式

     

    1:既然要采,当然找到有天气预报的站了,这个很好找,网上到处都是资源,只要你会采。

    比如百度,你搜索城市如广州,即会出现天气信息了,如图:

     

     

    比如腾讯soso,如下图。当然还有其它很多很多,只要看得到的,都可以采,不过最好找大站,稳定。

     

     

    2:采集类,一个好的采集类,事半功倍,以下出一个简化版,足够采集天气信息

    using  System; using  System.Text; using  System.Net; using  System.Text.RegularExpressions; namespace  CYQ.Tool {      ///   <summary>      ///  作者:路过秋天      ///  博客: http://cyq1162.cnblogs.com      ///   </summary>      public   class  GatherHelper     {          ///   <summary>          ///  返回获取的目标地址HTML全部代码          ///   </summary>          ///   <param name="strUrl"> 目标地址 </param>          ///   <returns></returns>          public   static   string  GetHtmlCode( string  pageUrl, Encoding encoding)         {              try             {                  // 返回目标页HTML代码                 WebClient webclient  =   new  WebClient();                 webclient.Credentials  =  CredentialCache.DefaultCredentials;                  byte [] buffer  =  webclient.DownloadData(pageUrl);                  string  HtmlCode  =  encoding.GetString(buffer);                 webclient.Dispose();     // 释放WebClient资源                  return  HtmlCode;             }              catch             {                  return   string .Empty;             }         }          #region  内容截取分析          ///   <summary>          ///  返回根据内容开始及结束代码分析出内容          ///   </summary>          ///   <param name="ContentCode"> 内容代码 </param>          ///   <param name="StartCode"> 内容所在开始代码 </param>          ///   <param name="EndCode"> 内容所在结束代码 </param>          ///   <param name="index"> 取第几条[从1开始] </param>          ///   <returns></returns>          public   static   string  GetContent( string  contentCode,  string  startCode,  string  endCode,  int  index)         {              string [] matchItems  =   null ;              return  GetContent(contentCode, startCode, endCode, index,  out  matchItems);         }          public   static   string  GetContent( string  contentCode,  string  startCode,  string  endCode,  int  index,  out   string [] matchItems)         {             matchItems  =   null ;              if  ( string .IsNullOrEmpty(startCode)  &&   string .IsNullOrEmpty(endCode))             {                  return  contentCode;             }             Regex regObj  =   new  Regex(startCode  +   @" ([\S\s]*?) "   +  endCode, RegexOptions.Compiled  |  RegexOptions.IgnoreCase);             MatchCollection matchItemList  =  regObj.Matches(contentCode);              if  (matchItemList  !=   null   &&  matchItemList.Count  >=  index)             {                 matchItems  =   new   string [matchItemList.Count];                  for  ( int  i  =   0 ; i  <  matchItemList.Count; i ++ )                 {                     matchItems[i]  =  matchItemList[i].Groups[ 1 ].Value;                 }                 index  =  index  >   0   ?  index  -   1  :  0 ;                  return  matchItemList[index].Groups[ 1 ].Value;             }              return   string .Empty;         }          #endregion     } }

     

    3:编写天气预报实体类,将采集的信息以实体返回,如果采集多个,返回就是List<实体>了

       public   class  WeatherInfo     {          private   string  imgUrl;          ///   <summary>          ///  天气图片地址          ///   </summary>          public   string  ImgUrl         {              get  {  return  imgUrl; }              set  { imgUrl  =  value; }         }          private   string  wind;          ///   <summary>          ///  天气风力          ///   </summary>          public   string  Wind         {              get  {  return  wind; }              set  { wind  =  value; }         }          private   string  cityName;          ///   <summary>          ///  天气城市名称          ///   </summary>          public   string  CityName         {              get  {  return  cityName; }              set  { cityName  =  value; }         }          private   string  temperature;          ///   <summary>          ///  天气温度          ///   </summary>          public   string  Temperature         {              get  {  return  temperature; }              set  { temperature  =  value; }         }          private   string  description;          ///   <summary>          ///  天气说明          ///   </summary>          public   string  Description         {              get  {  return  description; }              set  { description  =  value; }         }

     

    4:编写采集Soso的天气预报类

     

    A:新建采集天气预报类:WeatherSearch

         ///   <summary>      ///  作者:路过秋天      ///  博客: http://cyq1162.cnblogs.com      ///   </summary>      public   class  WeatherSearch     {          ///   <summary>          ///  数据采集来源于腾信搜搜天气预报          ///   </summary>          ///   <param name="cityName"></param>          ///   <returns></returns>          public   static  WeatherInfo Get( string  cityName)         {                // 待实现         }          private   static  WeatherInfo GetFormCache( string  cityName, string  key)         {              object  weather  =  HttpContext.Current.Cache.Get(key);              if  (weather != null )             {                  return  weather  as  WeatherInfo;             }              return   null ;         }     }

     

    重要说明:

    采集一次后,记得缓存起来,不然每次访问都现采,刷刷就被soso给封了,切身经历啊。

     

    B:Get函数分解:

    1:先读取缓存,注意缓存Key用日期做key,可以方便缓存今天和删除昨天的缓存。

             public   static  WeatherInfo Get( string  cityName) // 中文城市名称         {              if  ( string .IsNullOrEmpty(cityName))             {                  return   null ;             }              string  todayKey  =  cityName  +  DateTime.Now.ToString( " yyyyMMdd " );             WeatherInfo weather  =  GetFormCache(cityName, todayKey);              if  (weather  ==   null )             {                  // 待实现             }         }

    2:读不到缓存就现采了,调用采集类

      if  (weather  ==   null )   {                 weather  =   new  WeatherInfo();                 weather.CityName  =  cityName;                 cityName  =  System.Web.HttpUtility.UrlEncode(cityName  +   " 天气 " , Encoding.GetEncoding( " gb2312 " ));                  string  url  =   " http://www.soso.com/q?num=1&w= "   +  cityName;                  // 采集所有html                  string  html  =  GatherHelper.GetHtmlCode(url, Encoding.GetEncoding( " gb2312 " ));                  // 接下来待实现   }

    说明:

    这里城市要用中文编码传过去,至于url,是我发现的最简洁的参数,现在已把搜搜的搜索页面的全html抓回来了,接下来就是分离出想要的信息。

    3:分析html,缩小范围,对于一大堆html,我们只要这一部分

    <!-- 上面的被省略 --> < div  class ="w_main" >                                              < ol >                                                  < li  class ="w_space"  title ="北风4-5级" >< span > 今天(周五) </ span >                                                      < img  src ="http://cache.soso.com/zdq/wea/s_a3.png"  onload ="setPng(this,48,48)"   />                                                      < span > 21 / 28 < em > ° </ em > C </ span >< span  class ="w_w" > 多云转阵雨 </ span >   </ li >                                                  < li  title ="北风3-4级" >< span > 明天(周六) </ span >                                                      < img  src ="http://cache.soso.com/zdq/wea/s_a3.png"  onload ="setPng(this,48,48)"   />                                                      < span > 22 / 28 < em > ° </ em > C </ span >< span  class ="w_w" > 多云转阵雨 </ span >   </ li >                                                  < li  title ="微风" >< span > 后天(周日) </ span >                                                      < img  src ="http://cache.soso.com/zdq/wea/s_a33.png"  onload ="setPng(this,48,48)"   />                                                      < span > 18 / 29 < em > ° </ em > C </ span >< span  class ="w_w" > 多云 </ span >   </ li >                                              </ ol > </ div > <!-- 下面的也被省略 -->

    说明:

    我们使用GetContent方法可以非常方便的缩小范围,只要找到唯一的起始标签和结束标签,不会正则,也一样截取。

    4:使用GetContent步步截取所需要信息

    if  ( string .IsNullOrEmpty(html)) {  return   null ; }                  // 缩小范围                 html  =  GatherHelper.GetContent(html,  " <div class=\ " w_main\ " > " " </div> " 1 );                  if  ( string .IsNullOrEmpty(html)) {  return   null ; }                  // 说明                 weather.Description  =  GatherHelper.GetContent(html,  " <span class=\ " w_w\ " > " " </span> " 1 );                  // 图片                 weather.ImgUrl  =  GatherHelper.GetContent(html,  " <img src=\ "" " \ "" 1 );                                  // 风向                 weather.Wind = GatherHelper.GetContent(html,  " title=\ "" " \ "" 1 );                  // 温度                 weather.Temperature  =  GatherHelper.GetContent(html,  " /> <s pan> " " <em> " 1 );

    5:存入缓存并清除昨天的缓存信息[看需要展示几天的信息了]

                    HttpContext.Current.Cache.Insert(todayKey, weather);                  string  yesterdayKey  =  cityName  +  DateTime.Now.AddDays( - 1 ).ToString( " yyyyMMdd " );                  if  (HttpContext.Current.Cache.Get(yesterdayKey) != null )                 {                     HttpContext.Current.Cache.Remove(yesterdayKey);                 }

     

    5:界面调用

      protected   void  Page_Load( object  sender, EventArgs e)     {         WeatherInfo info  =  WeatherSearch.Get( " 广州 " );          if  (info  !=   null )         {             litCity.Text  =  info.CityName;             litDescprtion.Text  =  info.Description;             imgUrl.ImageUrl  =  info.ImgUrl;             litWind.Text  =  info.Wind;             litTemperature.Text = info.Temperature;         }     }

     

    6:调用结果

     

     

    最后结言:

    本博没有快速评论通道,大伙积极点下手。 再不济手动复制-》“此文不错,值的推荐”!! -_-...!!! IE6好卡,鼠标又发神经,单击双击混在一起,本文写起来好辛苦~~

     

     

    本文示例下载地址:CYQ.Data 轻量数据层之路 bug反馈、优化建议、最新框架下载

     

    版权声明:本文原创发表于博客园,作者为路过秋天,原文链接:

    http://www.cnblogs.com/cyq1162/archive/2010/10/22/1858680.html

    相关资源:敏捷开发V1.0.pptx
    最新回复(0)