程序员

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

作者:admin 2021-05-12 我要评论

百度了一下,找了点别人的方法改进了一下。 获取天气网址:http://www.weather.com.cn/html/weather/101210701.shtml这里是温州的,当然其他城市自己搜索一下,...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)
百度了一下,找了点别人的方法改进了一下。
获取天气网址: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+="&nbsp;<img src=\"http://m.weather.com.cn/img/c"+result.weatherinfo.img1+".gif\"/>&nbsp;"//天气图片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代码:
复制代码 代码如下:

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

这个是用网络上常见的asp抓取,当然也可以换成.net抓取或者其他。附上demo

原文链接:https://m.jb51.net/article/24098.htm

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • asp下以Json获取中国天气网天气的代码

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

  • Active Server Pages 错误 &#39;AS

    Active Server Pages 错误 &#39;AS

  • asp 在线备份与恢复sqlserver数据库的

    asp 在线备份与恢复sqlserver数据库的

  • Ajax向后台传json格式的数据出现415错

    Ajax向后台传json格式的数据出现415错