ASP下ajax抓取中国天气网天气的代码


ASP #ajax #采集2012-05-11 11:47
百度了一下,找了点别人的方法改进了一下。 
获取天气网址:http://www.weather.com.cn/html/weather/101210701.shtml这里是温州的,当然其他城市自己搜索一下,换一下ID。 
由于是写入cookies记录当天天气,所有需要在站点下浏览。 
js代码: 
02var COOKIE_info = "COOKIE_info";
03var date = new Date();
04var Hours=24-date.getHours()-1;
05var Mins=60-date.getMinutes()-1;
06var Secs=60-date.getSeconds();
07date.setTime(date.getTime() + (1 * Hours * 60 * 60 * 1000) + (1 * 1 * Mins * 60 * 1000) + (1 * 1 * 1 * Secs * 1000));
08var RD_cookie_info= $.cookie(COOKIE_info);
09$(function(){
10if(RD_cookie_info==null)
11{
12Getdata(Url);
13}
14else{
15$("#weather").html(RD_cookie_info);
16}
17})
18 
19 
20function Getdata(Url)
21{
22$.ajax({
23type:"GET",
24cache: "false",
25url: "AjaxGet.asp",
26data:"Url="+Url,
27dataType: "html",
28error: function(){$("#weather").html("读取失败...请刷新重试!");},
29success: function(json){
30var t = '('+json+')';
31var result = eval(t);
32var Getinfo="";
33Getinfo+=result.weatherinfo.date_y+" ";//年月日
34Getinfo+=result.weatherinfo.date+" ";//农历年月日
35Getinfo+=result.weatherinfo.week+" ";//星期几
36Getinfo+="<br />";//换行
37Getinfo+=result.weatherinfo.city;//获取城市名称
38Getinfo+=" <img src=\"http://m.weather.com.cn/img/c"+result.weatherinfo.img1+".gif\"/> "//天气图片1
39Getinfo+="<img src=\"http://m.weather.com.cn/img/c"+result.weatherinfo.img2+".gif\"/> ";//天气图片2
40Getinfo+=result.weatherinfo.weather1+" ";//天气情况
41Getinfo+=result.weatherinfo.temp1;//温度
42$.cookie(COOKIE_info,Getinfo, { path: '/', expires: date });
43$("#weather").html(Getinfo);
44}
45});
46}

html代码: 
1<div id="weather" style="font-size:12px;"></div>

相关文章

粤ICP备11097351号-1