菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

免费分享一个天气API接口及使用示例

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:js教程 jsapi JS请求

一个免费的天气API接口是心知天气。它提供了全球天气预报、实况、生活指数等信息,支持多种数据格式和自定义参数。使用示例如下: 1. 获取全球城市天气信息: ```python import requests def get_weather(city): api_key = "your_api_key" url = f"https://api.seniverse.com/v3/weather/now.json?key={api_key}&location={city}&language=zh-Hans&unit=c" response = requests.get(url) data = response.json() return data city = "北京" weather_data = get_weather(city) print(weather_data) ``` 2. 获取指定城市的实时天气信息: ```python def get_realtime_weather(city): api_key

网上的大多数天气接口要么是收费的,要么只支持 iframe 嵌入方式,根本就没法用……

今天“不小心”从 360 那挖到了一个支持 JSON 的天气接口,支持自动判断地区,获取最近五天的天气,支持天气相关信息展示。非常好用!

接口请求格式如下:

http://cdn.weather.hao.360.cn/sed_api_weather_info.php?app=360chrome&code=【地区编码】&_jsonp=【jsonp回调函数】

其中的 地区编码 与中国天气网的地区编码是一样的。如果不设置这个参数,则默认显示本地的天气状况。

简易的调用示例源码如下:(请自行进行界面美化)

<!DOCTYPEhtml><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/><title>天气接口使用示例</title><style>body{font-family:microsoftyahei;}</style><scriptsrc="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script></head><body><divid="output"></div><scripttype="text/javascript"charset="utf-8">$.ajax({type:"GET",url:"http://cdn.weather.hao.360.cn/sed_api_weather_info.php?app=360chrome",dataType:"jsonp",jsonp:"_jsonp",//参数名success:function(jsonData){varhtml;html='数据更新时间:'+jsonData.pubdate+''+jsonData.pubtime+'<br>';html+='地区:'+jsonData.area[0][0]+''+jsonData.area[1][0]+''+jsonData.area[2][0]+'<br>';html+='天气情况:<br>';for(vari=0;i<jsonData.weather.length;i++){html+=jsonData.weather[i].date+'<br>';if(jsonData.weather[i].info.dawn!==undefined){html+='早晨天气:'+jsonData.weather[i].info.dawn[1]+'气温:'+jsonData.weather[i].info.dawn[0]+'~'+jsonData.weather[i].info.dawn[2]+'℃'+jsonData.weather[i].info.dawn[3]+''+jsonData.weather[i].info.dawn[4]+'<br>';}if(jsonData.weather[i].info.day!==undefined){html+='白天天气:'+jsonData.weather[i].info.day[1]+'气温:'+jsonData.weather[i].info.day[0]+'~'+jsonData.weather[i].info.day[2]+'℃'+jsonData.weather[i].info.day[3]+''+jsonData.weather[i].info.day[4]+'<br>';}if(jsonData.weather[i].info.night!==undefined){html+='夜间天气:'+jsonData.weather[i].info.night[1]+'气温:'+jsonData.weather[i].info.night[0]+'~'+jsonData.weather[i].info.night[2]+'℃'+jsonData.weather[i].info.night[3]+''+jsonData.weather[i].info.night[4]+'<br>';}html+='<br>';}html+='穿衣:<br>';html+='【'+jsonData.life.info.chuanyi[0]+'】'+jsonData.life.info.chuanyi[1]+'<br><br>';html+='感冒:<br>';html+='【'+jsonData.life.info.ganmao[0]+'】'+jsonData.life.info.ganmao[1]+'<br><br>';html+='空调:<br>';html+='【'+jsonData.life.info.kongtiao[0]+'】'+jsonData.life.info.kongtiao[1]+'<br><br>';html+='污染:<br>';html+='【'+jsonData.life.info.wuran[0]+'】'+jsonData.life.info.wuran[1]+'<br><br>';html+='洗车:<br>';html+='【'+jsonData.life.info.xiche[0]+'】'+jsonData.life.info.xiche[1]+'<br><br>';html+='运动:<br>';html+='【'+jsonData.life.info.yundong[0]+'】'+jsonData.life.info.yundong[1]+'<br><br>';html+='紫外线:<br>';html+='【'+jsonData.life.info.ziwaixian[0]+'】'+jsonData.life.info.ziwaixian[1]+'<br><br>';html+='PM2.5:'+jsonData.pm25.pm25[0];$("#output").html(html);}});</script></body></html>
相关专题
评论
建站知识
建站知识
使用技巧
调试安装
运营推广