《小偷程序:自动获取百度天气预报》

在做项目的过程中,需要用到一个类似天气预报的功能,以前不懂,以为那个是需要自己手工去更新的,在新闻小偷遍地的今天,越来越觉得以前自己的想法是错误的,简直是很幼稚。这里给出实现天气预报抓取功能的全部代码,假如有更好的办法,请与我联系。
代码如下:

        /// <summary>
        
/// 获取当天的天气情况
        
/// </summary>
        
/// <param name="City"></param>
        
/// <returns></returns>

        public static string ThiefWeather(string City)
        
{
            
string TodayWeather;
            
string thisDate = DateTime.Today.ToString("yyyyMMdd");
            
if (!FObject.IsExist(HttpContext.Current.Server.MapPath("/Cache/Baidu/" + thisDate + ".lic"), FsoMethod.File))
            
{
                
try
                
{
                    Uri uri 
= new Uri("http://www.baidu.com/s?wd=" + HttpContext.Current.Server.UrlEncode(City + "天气预报").ToUpper() + "&cl=3");
                    WebRequest wreq 
= WebRequest.Create(uri);
                    HttpWebResponse wresp 
= (HttpWebResponse)wreq.GetResponse();
                    Stream s 
= wresp.GetResponseStream();
                    StreamReader objReader 
= new StreamReader(s, System.Text.Encoding.Default);
                    
string HTML = objReader.ReadToEnd();
                    TodayWeather 
= GetRegValue("city=" + HttpContext.Current.Server.UrlEncode(City).ToUpper() + "\" target=\"_blank\">" + City + "</a>(.+?)\n", HTML);
                    FObject.WriteFile(HttpContext.Current.Server.MapPath("/Cache/Baidu/" + thisDate + ".lic"), TodayWeather);
                }

                
catch
                
{
                    TodayWeather 
= "";
                }

            }

            
else
            
{
                TodayWeather 
= HttpContext.Current.Server.HtmlDecode(Licensed.GetLicenseData("/Cache/Baidu/" + thisDate + ".lic"));
            }

            
return TodayWeather;
        }

        
/// <summary>
        
/// 正则表达式获取对应数据
        
/// </summary>
        
/// <param name="RegexString"></param>
        
/// <param name="RemoteStr"></param>
        
/// <returns></returns>

        private static string GetRegValue(string RegexString, string RemoteStr)
        
{
            
string MatchVale = "";
            Regex r 
= new Regex(RegexString);
            Match m 
= r.Match(RemoteStr);
            
if (m.Success)
            
{
                MatchVale 
= m.Groups[1].ToString();
            }

            
return MatchVale;
        }

说下实现的思路吧,我想对于做程序的来说,其实思路是更加的重要,很多时候,发现很多人技术是可以的,就是缺少具体去实现他的思路,以至与我以前一个老师,是做软件开发的,做到后来一直在为软件的更新维护发愁,其实只要做个在线更新的功能就可以了,而这个思路,我好像却是来源与玩传奇的经历,因为那个时候登陆器是老是需要更新升级,呵呵。

编程思路:
抓取百度的天气预报的页面返回结果-->使用正则表达式进行字符分割,目的是为了实现只要的内容-->判断当日该天气预报文件是否存在(每天第一个用户会自动的去更新当日的天气预报,这里这样做的目的是为了提高网站的访问速度)-->在页面需要显示的地方去读取该文件就可以了。
posted on 2007-08-21 22:57  北极熊,我来了!  阅读(1619)  评论(4编辑  收藏  举报