好久没有写技术文章了,2010年工作太忙,奔波在国内各地,也没什么时间关注一些技术方面的事情,最近有一个项目封闭开发,有些时间来写些琐碎的东西,就当是整理下最近的东西吧,没什么技术价值,纯粹玩玩而已。
本篇是获取QQ天气预报信息,获取的是杭州的当天天气,如果需要获取未来三天以及其他城市的,做成配置即可,本人比较懒,简单的做该做的就OK了,下面是代码实现部分,纯粹抓取,没使用正则匹配,以前做过一个星座获取的,有时间的话一会一起贴下。
抓取天气信息部分
1 /// <summary>
2 /// 返回天气ArrayList
3 /// [0]日期
4 /// [1]白天天气图片
5 /// [2]夜晚天气图片
6 /// [3]白天天气描述
7 /// [4]夜晚天气描述
8 /// [5]白天天气温度
9 /// [6]夜晚天气温度
10 /// </summary>
11 /// <returns></returns>
12 private ArrayList QQWeather()
13 {
14 string Url = "http://weather.news.qq.com/inc/07_dc255.htm";
15 string GetStr = string.Empty;
16 WebClient MyWeb = new WebClient();
17 MyWeb.Credentials = CredentialCache.DefaultCredentials;
18 //MyWeb.Encoding = encode;
19 GetStr = Encoding.Default.GetString(MyWeb.DownloadData(Url));
20
21 ArrayList arr = new ArrayList(); //保存天气情况
22 string WeatherDetails = SubHtml(GetStr, "<strong>72小时天气预报</strong>", "</div></body>"); //获取出72小时内的天气
23 WeatherDetails = SubHtml(WeatherDetails, "<table width=\"175\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">", "</table>"); //获取出当天的天气
24 string Date = string.Empty; //日期
25 string WeatherDescription = string.Empty; //总体天气描述
26 string DayWeatherImage = ""; //白天天气图片
27 string NightWeatherImage = ""; //夜间天气图片
28 string DayWeatherDescription = string.Empty; //白天天气描述
29 string NightWeatherDescription = string.Empty; //夜间天气描述
30 string Temperature = string.Empty; //总体天气温度
31 string DayTemperature = string.Empty; //白天天气温度
32 string NightTemperature = string.Empty; //夜间天气温度
33
34 Date = SubHtml(WeatherDetails, "<td height=\"23\" align=\"center\" bgcolor=\"#EEEEEE\">", "</td>");
35 WeatherDescription = SubHtml(WeatherDetails, "<td height=\"57\" align=\"center\" bgcolor=\"#EEF3F8\">", "<br>");
36 NightWeatherImage = SubHtml(WeatherDetails, "<td height=\"60\" align=\"center\">\n<img width=\"44\" height=\"44\" src=\"", "\"> \n");
37 DayWeatherImage = SubHtml(WeatherDetails, "\t<img width=\"44\" height=\"44\" src=\"", "\">\n</td>");
38 Temperature = SubHtml(WeatherDetails, "<br>", "\n<br>");
39 if (WeatherDetails.IndexOf("转") != -1)
40 {
41 DayWeatherDescription = WeatherDescription.Split('转')[1];
42 NightWeatherDescription = WeatherDescription.Split('转')[0];
43 }
44 else
45 {
46 DayWeatherDescription = WeatherDescription;
47 NightWeatherDescription = WeatherDescription;
48 }
49 if (Temperature.IndexOf("~") != -1)
50 {
51 DayTemperature = Temperature.Split('~')[1];
52 NightTemperature = Temperature.Split('~')[0];
53 }
54 else
55 {
56 DayTemperature = Temperature;
57 NightTemperature = Temperature;
58 }
59
60 arr.Add(Date);
61 arr.Add(DayWeatherImage);
62 arr.Add(NightWeatherImage);
63 arr.Add(DayWeatherDescription);
64 arr.Add(NightWeatherDescription);
65 arr.Add(DayTemperature);
66 arr.Add(NightTemperature);
67
68 return arr;
69 }
2 /// 返回天气ArrayList
3 /// [0]日期
4 /// [1]白天天气图片
5 /// [2]夜晚天气图片
6 /// [3]白天天气描述
7 /// [4]夜晚天气描述
8 /// [5]白天天气温度
9 /// [6]夜晚天气温度
10 /// </summary>
11 /// <returns></returns>
12 private ArrayList QQWeather()
13 {
14 string Url = "http://weather.news.qq.com/inc/07_dc255.htm";
15 string GetStr = string.Empty;
16 WebClient MyWeb = new WebClient();
17 MyWeb.Credentials = CredentialCache.DefaultCredentials;
18 //MyWeb.Encoding = encode;
19 GetStr = Encoding.Default.GetString(MyWeb.DownloadData(Url));
20
21 ArrayList arr = new ArrayList(); //保存天气情况
22 string WeatherDetails = SubHtml(GetStr, "<strong>72小时天气预报</strong>", "</div></body>"); //获取出72小时内的天气
23 WeatherDetails = SubHtml(WeatherDetails, "<table width=\"175\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">", "</table>"); //获取出当天的天气
24 string Date = string.Empty; //日期
25 string WeatherDescription = string.Empty; //总体天气描述
26 string DayWeatherImage = ""; //白天天气图片
27 string NightWeatherImage = ""; //夜间天气图片
28 string DayWeatherDescription = string.Empty; //白天天气描述
29 string NightWeatherDescription = string.Empty; //夜间天气描述
30 string Temperature = string.Empty; //总体天气温度
31 string DayTemperature = string.Empty; //白天天气温度
32 string NightTemperature = string.Empty; //夜间天气温度
33
34 Date = SubHtml(WeatherDetails, "<td height=\"23\" align=\"center\" bgcolor=\"#EEEEEE\">", "</td>");
35 WeatherDescription = SubHtml(WeatherDetails, "<td height=\"57\" align=\"center\" bgcolor=\"#EEF3F8\">", "<br>");
36 NightWeatherImage = SubHtml(WeatherDetails, "<td height=\"60\" align=\"center\">\n<img width=\"44\" height=\"44\" src=\"", "\"> \n");
37 DayWeatherImage = SubHtml(WeatherDetails, "\t<img width=\"44\" height=\"44\" src=\"", "\">\n</td>");
38 Temperature = SubHtml(WeatherDetails, "<br>", "\n<br>");
39 if (WeatherDetails.IndexOf("转") != -1)
40 {
41 DayWeatherDescription = WeatherDescription.Split('转')[1];
42 NightWeatherDescription = WeatherDescription.Split('转')[0];
43 }
44 else
45 {
46 DayWeatherDescription = WeatherDescription;
47 NightWeatherDescription = WeatherDescription;
48 }
49 if (Temperature.IndexOf("~") != -1)
50 {
51 DayTemperature = Temperature.Split('~')[1];
52 NightTemperature = Temperature.Split('~')[0];
53 }
54 else
55 {
56 DayTemperature = Temperature;
57 NightTemperature = Temperature;
58 }
59
60 arr.Add(Date);
61 arr.Add(DayWeatherImage);
62 arr.Add(NightWeatherImage);
63 arr.Add(DayWeatherDescription);
64 arr.Add(NightWeatherDescription);
65 arr.Add(DayTemperature);
66 arr.Add(NightTemperature);
67
68 return arr;
69 }
截取的方法
字符串截取
1 /// <summary>
2 /// 字符截取
3 /// </summary>
4 /// <param name="data">源字符串</param>
5 /// <param name="f">起始字符</param>
6 /// <param name="b">结束字符</param>
7 /// <returns></returns>
8 private string SubHtml(string data, string f, string b)
9 {
10 int startIndex = 0;
11 int index = 0;
12 startIndex = data.IndexOf(f, index);
13 if (startIndex == -1)
14 {
15 return "";
16 }
17 index = data.IndexOf(b, startIndex);
18 if (index == -1)
19 {
20 return "";
21 }
22 return data.Substring(startIndex + f.Length, (index - startIndex) - f.Length);
23 }
2 /// 字符截取
3 /// </summary>
4 /// <param name="data">源字符串</param>
5 /// <param name="f">起始字符</param>
6 /// <param name="b">结束字符</param>
7 /// <returns></returns>
8 private string SubHtml(string data, string f, string b)
9 {
10 int startIndex = 0;
11 int index = 0;
12 startIndex = data.IndexOf(f, index);
13 if (startIndex == -1)
14 {
15 return "";
16 }
17 index = data.IndexOf(b, startIndex);
18 if (index == -1)
19 {
20 return "";
21 }
22 return data.Substring(startIndex + f.Length, (index - startIndex) - f.Length);
23 }
下面是输出测试
输出
1 ArrayList arrWeather = QQWeather();
2 string strWeather = string.Empty;
3 strWeather += arrWeather[0].ToString() + "<br />";
4 strWeather += "白天:<img src=\"" + arrWeather[1].ToString() + "\" border=0 /> ";
5 strWeather += arrWeather[3].ToString() + " ";
6 strWeather += arrWeather[5].ToString() + "<br />";
7 strWeather += "夜晚:<img src=\"" + arrWeather[2].ToString() + "\" border=0 /> ";
8 strWeather += arrWeather[4].ToString() + " ";
9 strWeather += arrWeather[6].ToString() + "<br />";
10 Response.Write(strWeather);
2 string strWeather = string.Empty;
3 strWeather += arrWeather[0].ToString() + "<br />";
4 strWeather += "白天:<img src=\"" + arrWeather[1].ToString() + "\" border=0 /> ";
5 strWeather += arrWeather[3].ToString() + " ";
6 strWeather += arrWeather[5].ToString() + "<br />";
7 strWeather += "夜晚:<img src=\"" + arrWeather[2].ToString() + "\" border=0 /> ";
8 strWeather += arrWeather[4].ToString() + " ";
9 strWeather += arrWeather[6].ToString() + "<br />";
10 Response.Write(strWeather);
下面是输出效果截图:
马上也要春节了,过完春节可能会有些时间,最近也在整理一套体系,有时间会多写的文章。