天气预报的获取
? 好久沒有寫技術文章了,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?????}
?
截取的方法
?
字符串截取 ?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?????}
?
?
下面是輸出測試
?
輸出 ?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);
?
下面是輸出效果截圖:
馬上也要春節了,過完春節可能會有些時間,最近也在整理一套體系,有時間會多寫的文章。
轉載于:https://www.cnblogs.com/saisky/archive/2011/01/27/1946304.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: mysql大数据优化要注意的细节
- 下一篇: Java编码安全规范