ASP下ajax抓取中国天气网天气的代码
ASP #ajax #采集2012-05-11 11:47
百度了一下,找了点别人的方法改进了一下。
获取天气网址:http://www.weather.com.cn/html/weather/101210701.shtml这里是温州的,当然其他城市自己搜索一下,换一下ID。
由于是写入cookies记录当天天气,所有需要在站点下浏览。
js代码:
01 | var Url=escape( "http://m.weather.com.cn/data/101210701.html" ); |
02 | var COOKIE_info = "COOKIE_info" ; |
03 | var date = new Date(); |
04 | var Hours=24-date.getHours()-1; |
05 | var Mins=60-date.getMinutes()-1; |
06 | var Secs=60-date.getSeconds(); |
07 | date.setTime(date.getTime() + (1 * Hours * 60 * 60 * 1000) + (1 * 1 * Mins * 60 * 1000) + (1 * 1 * 1 * Secs * 1000)); |
08 | var RD_cookie_info= $.cookie(COOKIE_info); |
09 | $( function (){ |
10 | if (RD_cookie_info== null ) |
11 | { |
12 | Getdata(Url); |
13 | } |
14 | else { |
15 | $( "#weather" ).html(RD_cookie_info); |
16 | } |
17 | }) |
18 |
19 |
20 | function Getdata(Url) |
21 | { |
22 | $.ajax({ |
23 | type: "GET" , |
24 | cache: "false" , |
25 | url: "AjaxGet.asp" , |
26 | data: "Url=" +Url, |
27 | dataType: "html" , |
28 | error: function (){$( "#weather" ).html( "读取失败...请刷新重试!" );}, |
29 | success: function (json){ |
30 | var t = '(' +json+ ')' ; |
31 | var result = eval(t); |
32 | var Getinfo= "" ; |
33 | Getinfo+=result.weatherinfo.date_y+ " " ; //年月日 |
34 | Getinfo+=result.weatherinfo.date+ " " ; //农历年月日 |
35 | Getinfo+=result.weatherinfo.week+ " " ; //星期几 |
36 | Getinfo+= "<br />" ; //换行 |
37 | Getinfo+=result.weatherinfo.city; //获取城市名称 |
38 | Getinfo+= " <img src=\"http://m.weather.com.cn/img/c" +result.weatherinfo.img1+ ".gif\"/> " //天气图片1 |
39 | Getinfo+= "<img src=\"http://m.weather.com.cn/img/c" +result.weatherinfo.img2+ ".gif\"/> " ;//天气图片2 |
40 | Getinfo+=result.weatherinfo.weather1+ " " ; //天气情况 |
41 | Getinfo+=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 > |
相关文章
- ASP仿PHP的一些常用函数 2012/05/11
- ASP获取远程网页的指定内容的函数 2012/05/11
- ASP生成SQL命令字符串类 2012/05/11
- ASP文本框换行显示方法 2012/05/11
- ASP页面支持cache的方法 2012/05/11
- ASP的JSON类使用方法 2012/05/11
- ASP的JSON类 2012/05/11
- ASP实现注册登录的代码 2012/05/11
- ASP实现ajax注册即时提示代码 2012/05/10
- ASP多级联动菜单效果代码 2012/05/10