百度了一下,找了點(diǎn)別人的方法改進(jìn)了一下。
獲取天氣網(wǎng)址:http://www.weather.com.cn/html/weather/101210701.shtml這里是溫州的,當(dāng)然其他城市自己搜索一下,換一下ID。
由于是寫入cookies記錄當(dāng)天天氣,所有需要在站點(diǎn)下瀏覽。
js代碼:
復(fù)制代碼 代碼如下:
var Url=escape("http://m.weather.com.cn/data/101210701.html");
var COOKIE_info = "COOKIE_info";
var date = new Date();
var Hours=24-date.getHours()-1;
var Mins=60-date.getMinutes()-1;
var Secs=60-date.getSeconds();
date.setTime(date.getTime() + (1 * Hours * 60 * 60 * 1000) + (1 * 1 * Mins * 60 * 1000) + (1 * 1 * 1 * Secs * 1000));
var RD_cookie_info= $.cookie(COOKIE_info);
$(function(){
if(RD_cookie_info==null)
{
Getdata(Url);
}
else{
$("#weather").html(RD_cookie_info);
}
})
function Getdata(Url)
{
$.ajax({
type:"GET",
cache: "false",
url: "AjaxGet.asp",
data:"Url="+Url,
dataType: "html",
error: function(){$("#weather").html("讀取失敗...請刷新重試!");},
success: function(json){
var t = '('+json+')';
var result = eval(t);
var Getinfo="";
Getinfo+=result.weatherinfo.date_y+"nbsp;";//年月日
Getinfo+=result.weatherinfo.date+"nbsp;";//農(nóng)歷年月日
Getinfo+=result.weatherinfo.week+"nbsp;";//星期幾
Getinfo+="br />";//換行
Getinfo+=result.weatherinfo.city;//獲取城市名稱
Getinfo+="nbsp;img src=\"http://m.weather.com.cn/img/c"+result.weatherinfo.img1+".gif\"/>nbsp;"http://天氣圖片1
Getinfo+="img src=\"http://m.weather.com.cn/img/c"+result.weatherinfo.img2+".gif\"/>nbsp;";//天氣圖片2
Getinfo+=result.weatherinfo.weather1+"nbsp;";//天氣情況
Getinfo+=result.weatherinfo.temp1;//溫度
$.cookie(COOKIE_info,Getinfo, { path: '/', expires: date });
$("#weather").html(Getinfo);
}
});
}
html代碼:
復(fù)制代碼 代碼如下:
div id="weather" style="font-size:12px;">/div>
這個(gè)是用網(wǎng)絡(luò)上常見的asp抓取,當(dāng)然也可以換成.net抓取或者其他。附上demo。
您可能感興趣的文章:- ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp
- asp實(shí)現(xiàn)讀取數(shù)據(jù)庫輸出json代碼
- asp對復(fù)雜json的解析一定要注意要點(diǎn)
- ASP JSON類文件的使用方法
- ASP JSON類源碼分享
- ASP Json Parser修正版
- ASP 處理JSON數(shù)據(jù)的實(shí)現(xiàn)代碼