今天心血来潮,看到腾讯天气网站的内容不错,就写了一个小程序,通过正则表达式分析网页内容,提取天气信息显示出来,然后定义一个计划任务定时弹出一个MessageBox显示天气信息,在上午上班时间显示今天天气,下午下班显示明天天气,感觉还不错。
数据来源于网页:http://weather.qq.com/,主要分析两个页面,从http://weather.qq.com/inc/ss292.htm可以看到今天的天气,从http://weather.qq.com/inc/dc292.htm可以看到明天及未来的天气,主要利用System.Text.RegularExpressions中的Regex对象分析提取信息,例如html内容“<td height="23" align="center">小于3级</td>”,利用下面的表达式align=\"center\">([^<]*)</td>分析就可以获得字符串“小于3级”,大致代码如下








最后,为了便于计划任务调用,用csc /target:winexe参数编译为winexe格式,然后加到计划任务中ok。
附:实现源代码https://files.cnblogs.com/midea0978/weather.rar
由于部分方法是.net 2.0中的,所以只能运行于.net 2.0环境,对于1.1则需要修改几个地方。