加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

asp下以Json获得中国天气网天气的代码

发布时间:2023-06-12 14:31:37 所属栏目:Asp教程 来源:
导读:这两天搞个天气预报,记得以前用.net版本有种是抓取百度天气写入txt,再读取。时间久了,txt很多。感觉不是很好。

百度了一下,找了点别人的方法改进了一下。

获取天气网址:http://www.weather.com.cn/html
这两天搞个天气预报,记得以前用.net版本有种是抓取百度天气写入txt,再读取。时间久了,txt很多。感觉不是很好。
 
百度了一下,找了点别人的方法改进了一下。
 
获取天气网址:http://www.weather.com.cn/html/weather/101210701.shtml这里是温州的,当然其他城市自己搜索一下,换一下ID。
 
由于是写入cookies记录当天天气,所有需要在站点下浏览。
 
js代码:
 
复制代码代码如下:
 
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+" ";//年月日
 
Getinfo+=result.weatherinfo.date+" ";//农历年月日
 
Getinfo+=result.weatherinfo.week+" ";//星期几
 
Getinfo+="<br />";//换行
 
Getinfo+=result.weatherinfo.city;//获取城市名称
 
Getinfo+=" <img src=/"http://m.weather.com.cn/img/c"+result.weatherinfo.img1+".gif/"/> "//天气图片1
 
Getinfo+="<img src=/"http://m.weather.com.cn/img/c"+result.weatherinfo.img2+".gif/"/> ";//天气图片2
 
Getinfo+=result.weatherinfo.weather1+" ";//天气情况
 
Getinfo+=result.weatherinfo.temp1;//温度
 
$.cookie(COOKIE_info,Getinfo, { path: '/', expires: date });
 
$("#weather").html(Getinfo);
 
}
 
});
 
}

html代码:
 
复制代码代码如下:
 
<div id="weather" style="font-size:12px;"></div>
 
这个是用网络上常见的asp抓取,当然也可以换成.net抓取或者其他。
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章