配合AJAX天气预报的WebService
AJAX实现雅虎天气预报的抓取
因为发现AJAX目前跨域有点问题,只能在IE里面设置才可以使用,所以自己就写了个简单ASP的webService,其实作用就是一个小偷程序,把雅虎的天气抓了过来,然后生成XML。先从这里开始吧,以后会考虑添加数据库,这样客户端写好后读取速度就很快了。
习惯了用JS写东西,一不小心ASP也全拿JS写的,呵呵,有时间再改成VB。
我再鄙视他一下,FLASH是N年前就搞过的,时间虽然丢的有点长了,但是在AW的帮助,慢慢就会有所起色。
总算平息了火,还算是又默默改变了一件事情。
运行结果:
因为发现AJAX目前跨域有点问题,只能在IE里面设置才可以使用,所以自己就写了个简单ASP的webService,其实作用就是一个小偷程序,把雅虎的天气抓了过来,然后生成XML。先从这里开始吧,以后会考虑添加数据库,这样客户端写好后读取速度就很快了。
习惯了用JS写东西,一不小心ASP也全拿JS写的,呵呵,有时间再改成VB。
我再鄙视他一下,FLASH是N年前就搞过的,时间虽然丢的有点长了,但是在AW的帮助,慢慢就会有所起色。
总算平息了火,还算是又默默改变了一件事情。
程序代码
1 <%@LANGUAGE="JavaScript" CODEPAGE="65001"%>
2 <%
3 Session.CodePage = "65001"
4 Response.contentType="text/html"
5 Response.Expires = 0
6 var N=Request("n")
7 var Num=Request("id")
8 var url="http://xml.weather.yahoo.com/forecastrss?p="+N+"&u=c&id="+Num;
9 var xmldoc=Server.CreateObject("Microsoft.Xmlhttp");
10 xmldoc.open('GET',url,false);
11 xmldoc.send(null);
12 var response = xmldoc.responseXML.documentElement;
13 var lastNode=response.selectNodes("//rss/channel/lastBuildDate");
14 var cityNode=response.selectNodes("//rss/channel/yweather:location");
15 var windNode=response.selectNodes("//rss/channel/yweather:wind");
16 var atmosphereNode=response.selectNodes("//rss/channel/yweather:atmosphere");
17 var astronomyNode=response.selectNodes("//rss/channel/yweather:astronomy");
18 var conditionNode=response.selectNodes("//rss/channel/item/yweather:condition");
19 var forecastNode=response.selectNodes("//rss/channel/item/yweather:forecast");
20 var lastV=lastNode(0).firstChild.nodeValue
21 //编辑属性
22 function attri(a,b,c){
23 return a(b).attributes.getNamedItem(c).value
24 }
25 var cityV=attri(cityNode,0,"city")
26 var chillV=attri(windNode,0,"chill")
27 var directionV=attri(windNode,0,"direction")
28 var speedV=attri(windNode,0,"speed")
29 var humidityV=attri(atmosphereNode,0,"humidity")
30 var visibilityV=attri(atmosphereNode,0,"visibility")
31 var sunriseV=attri(astronomyNode,0,"sunrise")
32 var sunsetV=attri(astronomyNode,0,"sunset")
33 var textV=attri(conditionNode,0,"text")
34 var codeV=attri(conditionNode,0,"code")
35 var tempV=attri(conditionNode,0,"temp")
36 //转换英文为中文
37 var cityCH=EN2CH(cityV)
38 function EN2CH(obj){
39 if(obj=="Wuhan"){return "武汉"}
40 else if(obj=="Nanchang"){return "南昌"}
41 else if(obj=="Haikou"){return "海口"}
42 else if(obj=="Beijing"){return "北京"}
43 else if(obj=="Shanghai"){return "上海"}
44 else if(obj=="Guangzhou"){return "广州"}
45 else if(obj=="Yinchuan"){return "银川"}
46 else if(obj=="Zhengzhou"){return "郑州"}
47 else if(obj=="Changsha"){return "长沙"}
48 else if(obj=="Hanzhong"){return "杭州"}
49 else if(obj=="Hong Kong"){return "香港"}
50 else if(obj=="Xin'an"){return "西安"}
51 else if(obj=="Chengdu"){return "成都"}
52 else if(obj=="Qingdao"){return "青岛"}
53 else if(obj=="Guiyang"){return "贵阳"}
54 else if(obj=="Jinan"){return "济南"}
55 else if(obj=="Hefei"){return "合肥"}
56 else if(obj=="Fuzhou"){return "福州"}
57 else if(obj=="Nanjing"){return "南京"}
58 else{return obj}
59 }
60 //风向函数
61 var windCH=wind(directionV)
62 function wind(a){
63 if(a>337 && a<=360){return "北风"};
64 else if(a>=0 && a<=22){return "北风"};
65 else if(a>22 && a<=67){return "东北风"};
66 else if(a>67 && a<=112){return "东风"};
67 else if(a>112 && a<=157){return "东南风"};
68 else if(a>157 && a<=202){return "南风"};
69 else if(a>202 && a<=247){return "西南风"};
70 else if(a>247 && a<=292){return "西风"};
71 else if(a>292 && a<=337){return "西北风"};
72 }
73 //能见度函数
74 visibility=vis(visibilityV)
75 function vis(a){
76 return a/100
77 }
78 //转换天气为中文
79 var textCH=tEN2CH(textV)
80 function tEN2CH(obj){
81 if(obj=="Clear"){return "晴朗"}
82 else if(obj=="Sunny"){return "晴天"}
83 else if(obj=="Partly Cloudy"){return "局部有云"}
84 else if(obj=="Mostly Clear"){return "晴"}
85 else if(obj=="Mostly Sunny"){return "小云"}
86 else if(obj=="Mostly Cloudy"){return "多云"}
87 else if(obj=="Cloudy"){return "云"}
88 else if(obj=="Sunny/Wind"){return "晴转云"}
89 else if(obj=="AM Clouds/PM Sun/Wind"){return "云转晴"}
90 else if(obj=="AM Showers"){return "晨雨"}
91 else{return obj}
92 }
93 //天气图片
94 var weaNum=weaimg(codeV)
95 function weaimg(obj){
96 return "<img src=\"http\://us\.i1\.yimg\.com/us\.yimg.com/i/us/we/52/"+obj+"\.gif\" />"
97 }
98 //星期替换
99 function dayEn(obj){
100 if(obj=="Sun"){return "星期日"}
101 else if(obj=="Mon"){return "星期一"}
102 else if(obj=="Tue"){return "星期二"}
103 else if(obj=="Wed"){return "星期三"}
104 else if(obj=="Thu"){return "星期四"}
105 else if(obj=="Fri"){return "星期五"}
106 else if(obj=="Sat"){return "星期六"}
107 else{return obj}
108 }
109 %>
110 <table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
111 <tr>
112 <td rowspan="6" align="center"><%=cityCH%><br />
113 <%=weaNum%><br />
114 <%=textCH%></td>
115 <td align="right">感觉:</td>
116 <td align="center"><%=chillV%>°</td>
117 </tr>
118 <tr style="background-color:#E6F5FF">
119 <td align="right">实际:</td>
120 <td align="center"><%=tempV%>°</td>
121 </tr>
122 <tr>
123 <td align="right">风向:</td>
124 <td align="center"><%=windCH%></td>
125 </tr>
126 <tr style="background-color:#E6F5FF">
127 <td align="right">风速:</td>
128 <td align="center"><%=speedV%>Kph</td>
129 </tr>
130 <tr>
131 <td align="right">湿度:</td>
132 <td align="center"><%=humidityV%>%</td>
133 </tr>
134 <tr style="background-color:#E6F5FF">
135 <td align="right">能见度:</td>
136 <td align="center"><%=visibility%>km</td>
137 </tr>
138 </table>
139 <table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
140 <tr>
141 <td colspan="2" align="center" class="time"><%=lastV%></td>
142 </tr>
143 <tr>
144 <td bgcolor="#FFFFCC">日出:<%=sunriseV%></td>
145 <td bgcolor="#FFCC00">日落:<%=sunsetV%></td>
146 </tr>
147 </table>
148 <table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
149 <tr>
150 <%
151 for(var i=0; i<2; i++){
152 var dayV=attri(forecastNode,i,"day")
153 var dateV=attri(forecastNode,i,"date")
154 var lowV=attri(forecastNode,i,"low")
155 var highV=attri(forecastNode,i,"high")
156 var texttV=attri(forecastNode,i,"text")
157 var codetV=attri(forecastNode,i,"code")
158 var dayCH=dayEn(dayV)
159 var texttCH=tEN2CH(texttV)
160 var weabmp=weaimg(codetV)
161 //var weatNum=weaimg(codetV)
162 %>
163 <td align="center"><%=dayCH%><br /><%=weabmp%><br /><%=texttCH%><br />最高<%=highV%>°<br />最低<%=lowV%>°</td>
164 <%
165 }
166 %>
167 </tr>
168 </table>
169 </html>
2 <%
3 Session.CodePage = "65001"
4 Response.contentType="text/html"
5 Response.Expires = 0
6 var N=Request("n")
7 var Num=Request("id")
8 var url="http://xml.weather.yahoo.com/forecastrss?p="+N+"&u=c&id="+Num;
9 var xmldoc=Server.CreateObject("Microsoft.Xmlhttp");
10 xmldoc.open('GET',url,false);
11 xmldoc.send(null);
12 var response = xmldoc.responseXML.documentElement;
13 var lastNode=response.selectNodes("//rss/channel/lastBuildDate");
14 var cityNode=response.selectNodes("//rss/channel/yweather:location");
15 var windNode=response.selectNodes("//rss/channel/yweather:wind");
16 var atmosphereNode=response.selectNodes("//rss/channel/yweather:atmosphere");
17 var astronomyNode=response.selectNodes("//rss/channel/yweather:astronomy");
18 var conditionNode=response.selectNodes("//rss/channel/item/yweather:condition");
19 var forecastNode=response.selectNodes("//rss/channel/item/yweather:forecast");
20 var lastV=lastNode(0).firstChild.nodeValue
21 //编辑属性
22 function attri(a,b,c){
23 return a(b).attributes.getNamedItem(c).value
24 }
25 var cityV=attri(cityNode,0,"city")
26 var chillV=attri(windNode,0,"chill")
27 var directionV=attri(windNode,0,"direction")
28 var speedV=attri(windNode,0,"speed")
29 var humidityV=attri(atmosphereNode,0,"humidity")
30 var visibilityV=attri(atmosphereNode,0,"visibility")
31 var sunriseV=attri(astronomyNode,0,"sunrise")
32 var sunsetV=attri(astronomyNode,0,"sunset")
33 var textV=attri(conditionNode,0,"text")
34 var codeV=attri(conditionNode,0,"code")
35 var tempV=attri(conditionNode,0,"temp")
36 //转换英文为中文
37 var cityCH=EN2CH(cityV)
38 function EN2CH(obj){
39 if(obj=="Wuhan"){return "武汉"}
40 else if(obj=="Nanchang"){return "南昌"}
41 else if(obj=="Haikou"){return "海口"}
42 else if(obj=="Beijing"){return "北京"}
43 else if(obj=="Shanghai"){return "上海"}
44 else if(obj=="Guangzhou"){return "广州"}
45 else if(obj=="Yinchuan"){return "银川"}
46 else if(obj=="Zhengzhou"){return "郑州"}
47 else if(obj=="Changsha"){return "长沙"}
48 else if(obj=="Hanzhong"){return "杭州"}
49 else if(obj=="Hong Kong"){return "香港"}
50 else if(obj=="Xin'an"){return "西安"}
51 else if(obj=="Chengdu"){return "成都"}
52 else if(obj=="Qingdao"){return "青岛"}
53 else if(obj=="Guiyang"){return "贵阳"}
54 else if(obj=="Jinan"){return "济南"}
55 else if(obj=="Hefei"){return "合肥"}
56 else if(obj=="Fuzhou"){return "福州"}
57 else if(obj=="Nanjing"){return "南京"}
58 else{return obj}
59 }
60 //风向函数
61 var windCH=wind(directionV)
62 function wind(a){
63 if(a>337 && a<=360){return "北风"};
64 else if(a>=0 && a<=22){return "北风"};
65 else if(a>22 && a<=67){return "东北风"};
66 else if(a>67 && a<=112){return "东风"};
67 else if(a>112 && a<=157){return "东南风"};
68 else if(a>157 && a<=202){return "南风"};
69 else if(a>202 && a<=247){return "西南风"};
70 else if(a>247 && a<=292){return "西风"};
71 else if(a>292 && a<=337){return "西北风"};
72 }
73 //能见度函数
74 visibility=vis(visibilityV)
75 function vis(a){
76 return a/100
77 }
78 //转换天气为中文
79 var textCH=tEN2CH(textV)
80 function tEN2CH(obj){
81 if(obj=="Clear"){return "晴朗"}
82 else if(obj=="Sunny"){return "晴天"}
83 else if(obj=="Partly Cloudy"){return "局部有云"}
84 else if(obj=="Mostly Clear"){return "晴"}
85 else if(obj=="Mostly Sunny"){return "小云"}
86 else if(obj=="Mostly Cloudy"){return "多云"}
87 else if(obj=="Cloudy"){return "云"}
88 else if(obj=="Sunny/Wind"){return "晴转云"}
89 else if(obj=="AM Clouds/PM Sun/Wind"){return "云转晴"}
90 else if(obj=="AM Showers"){return "晨雨"}
91 else{return obj}
92 }
93 //天气图片
94 var weaNum=weaimg(codeV)
95 function weaimg(obj){
96 return "<img src=\"http\://us\.i1\.yimg\.com/us\.yimg.com/i/us/we/52/"+obj+"\.gif\" />"
97 }
98 //星期替换
99 function dayEn(obj){
100 if(obj=="Sun"){return "星期日"}
101 else if(obj=="Mon"){return "星期一"}
102 else if(obj=="Tue"){return "星期二"}
103 else if(obj=="Wed"){return "星期三"}
104 else if(obj=="Thu"){return "星期四"}
105 else if(obj=="Fri"){return "星期五"}
106 else if(obj=="Sat"){return "星期六"}
107 else{return obj}
108 }
109 %>
110 <table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
111 <tr>
112 <td rowspan="6" align="center"><%=cityCH%><br />
113 <%=weaNum%><br />
114 <%=textCH%></td>
115 <td align="right">感觉:</td>
116 <td align="center"><%=chillV%>°</td>
117 </tr>
118 <tr style="background-color:#E6F5FF">
119 <td align="right">实际:</td>
120 <td align="center"><%=tempV%>°</td>
121 </tr>
122 <tr>
123 <td align="right">风向:</td>
124 <td align="center"><%=windCH%></td>
125 </tr>
126 <tr style="background-color:#E6F5FF">
127 <td align="right">风速:</td>
128 <td align="center"><%=speedV%>Kph</td>
129 </tr>
130 <tr>
131 <td align="right">湿度:</td>
132 <td align="center"><%=humidityV%>%</td>
133 </tr>
134 <tr style="background-color:#E6F5FF">
135 <td align="right">能见度:</td>
136 <td align="center"><%=visibility%>km</td>
137 </tr>
138 </table>
139 <table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
140 <tr>
141 <td colspan="2" align="center" class="time"><%=lastV%></td>
142 </tr>
143 <tr>
144 <td bgcolor="#FFFFCC">日出:<%=sunriseV%></td>
145 <td bgcolor="#FFCC00">日落:<%=sunsetV%></td>
146 </tr>
147 </table>
148 <table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
149 <tr>
150 <%
151 for(var i=0; i<2; i++){
152 var dayV=attri(forecastNode,i,"day")
153 var dateV=attri(forecastNode,i,"date")
154 var lowV=attri(forecastNode,i,"low")
155 var highV=attri(forecastNode,i,"high")
156 var texttV=attri(forecastNode,i,"text")
157 var codetV=attri(forecastNode,i,"code")
158 var dayCH=dayEn(dayV)
159 var texttCH=tEN2CH(texttV)
160 var weabmp=weaimg(codetV)
161 //var weatNum=weaimg(codetV)
162 %>
163 <td align="center"><%=dayCH%><br /><%=weabmp%><br /><%=texttCH%><br />最高<%=highV%>°<br />最低<%=lowV%>°</td>
164 <%
165 }
166 %>
167 </tr>
168 </table>
169 </html>
运行结果:
我来自:向东博客