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

简介:

前言:

今天 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 > &deg; </ 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 > &deg; </ 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 > &deg; </ 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

相关文章
【论文实操】从ACNet中得到启发:非对称卷积块的使用可以有效提高ACC。即插即用!
【论文实操】从ACNet中得到启发:非对称卷积块的使用可以有效提高ACC。即插即用!
632 0
【论文实操】从ACNet中得到启发:非对称卷积块的使用可以有效提高ACC。即插即用!
|
移动开发 JavaScript 小程序
从入门到实践:Uni-app跨平台开发与应用
从入门到实践:Uni-app跨平台开发与应用
714 1
|
Unix Linux 程序员
Linux必知词汇:GNU通用公共许可证 GPL(GNU General Public License)
Linux必知词汇:GNU通用公共许可证 GPL(GNU General Public License)
2944 0
|
机器学习/深度学习 人工智能 测试技术
阿里云百炼已上线超强推理开源模型QwQ-32B,尺寸更小,性能比肩DeepSeek满血版
通义千问团队推出了320亿参数的QwQ-32B模型,通过大规模强化学习和多阶段训练,在数学、编程及通用能力上达到或超越了DeepSeek-R1等先进模型。QwQ-32B模型已在阿里云百炼上线,支持API调用,用户可通过官方文档了解详细使用方法。未来,团队将继续探索智能体与RL集成,推动人工通用智能的发展。
8676 0
|
数据采集 存储 数据可视化
【优秀python数据分析案例】基于python的中国天气网数据采集与可视化分析的设计与实现
本文介绍了一个基于Python的中国天气网数据采集与可视化分析系统,通过requests和BeautifulSoup库实现数据爬取,利用matplotlib、numpy和pandas进行数据可视化,提供了温湿度变化曲线、空气质量图、风向雷达图等分析结果,有效预测和展示了未来天气信息。
3235 3
如何随机切换代理IP以避免被封禁?
如何随机切换代理IP以避免被封禁?
365 1
|
机器学习/深度学习 数据采集 人工智能
数据工作中的自动化与AI融合实践
【8月更文第13天】随着大数据和人工智能(AI)技术的发展,数据处理和分析变得越来越重要。本文将探讨如何通过自动化工具和AI技术来优化数据处理流程,包括数据清洗、特征工程、模型训练以及结果可视化等步骤。我们将使用Python编程语言及其相关库(如Pandas、Scikit-learn和TensorFlow)作为实现手段。
821 0
|
监控 数据挖掘 数据安全/隐私保护
ERP系统中的应收应付管理与风险控制解析
【7月更文挑战第25天】 ERP系统中的应收应付管理与风险控制解析
667 2
|
数据采集 存储 关系型数据库
基于python的中国气象局气象数据采集,可以作为数据集使用
本文介绍了一个基于Python的中国气象局气象数据采集项目,使用requests、lxml等技术将天气数据爬取并存储至MySQL数据库,可用于数据集创建。
1024 0
|
存储 安全 C语言
C语言中的共用体与结构体的区别
C语言中的共用体与结构体的区别
324 3