温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

百度API 免费接口获取天气预报

发布时间:2020-08-06 05:38:12 来源:网络 阅读:5238 作者:大大果 栏目:开发技术

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Document</title>

<style>

#result{width: 800px;border:1px solid #ddd;margin-top: 30px;background: #aaa;display: none;}

#result p{height: 25px;line-height: 25px;padding-left: 20px;color: #fff;font-weight: bold;font-size: 16px;}

#result p span{padding: 0 10px;display: inline-block;width: 120px;text-align:center;height: 25px;line-height: 25px;}

#result p span.temp{width: 250px;text-align: left;margin-left: 20px;}

</style>

<script src="Jquery/jquery-1.12.4.js"></script>

<script>

$(function(){

$(window).on('load',function(){

$.ajax({

'type':'get',

'url':'http://apis.baidu.com/heweather/weather/free?city=beijing',

'headers':{'apikey':'2c6d2927ef76200697018e989f6afbbb'},

'success':function(data){

$('#result').show();

data = data['HeWeather data service 3.0'][0];

var basic_info = '城市:' + data.basic.cnty + '&nbsp;' + data.basic.city;

var tianqi = data.daily_forecast;

$('<p><strong>' + basic_info + '</strong></p>').appendTo('#result');

$('<p><span>日期</span><span>气温</span><span class="temp">天气</span></p>').appendTo('#result');

$.each(tianqi,function(index,element){

var html = '';

var date = element.date;

var temperature = element.tmp.min + '&#176;C - ' + element.tmp.max + '&#176;C';

var w1 = element.cond.txt_d;

var w2 = element.cond.txt_n;

var weather;

if(w1 == w2){

weather = w1;

}

else{

weather = w1 + ' 转 ' + w2;

}

html += '<span>'+ date +'</span>' + '<span>'+ temperature +'</span>' + '<span class="temp">'+ weather +'</span>';

$('<p>'+ html +'</p>').appendTo('#result');

});

}

});

});


$('#query').on('click',function(){

$('#result').hide().empty();

var city = $.trim($('#city').val());

$.ajax({

'type':'get',

'url':'http://apis.baidu.com/heweather/weather/free',

'data':{'city':city},

'headers':{'apikey':'2c6d2927ef76200697018e989f6afbbb'},

'success':function(data){

$('#city').val('');

data = data['HeWeather data service 3.0'][0];

console.log(data);

var basic_info = '城市:' + data.basic.cnty + '&nbsp;' + data.basic.city;

var tianqi = data.daily_forecast;

$('<p><strong>' + basic_info + '</strong></p>').appendTo('#result');

$('<p><span>日期</span><span>气温</span><span>天气</span></p>').appendTo('#result');

$.each(tianqi,function(index,element){

var html = '';

var date = element.date;

var temperature = element.tmp.min + '℃ - ' + element.tmp.max + '℃';

var w1 = element.cond.txt_d;

var w2 = element.cond.txt_n;

var weather;

if(w1 == w2){

weather = w1;

}

else{

weather = w1 + ' 转 ' + w2;

}

html += '<span>'+ date +'</span>' + '<span>'+ temperature +'</span>' + '<span>'+ weather +'</span>';

$('<p>'+ html +'</p>').appendTo('#result');

$('#result').show();

});

}

});

});

});

</script>

</head>

<body>

<input type="text" placeholder="请输入城市" id="city" />

<input type="button" value="查询" id="query" />

<div id="result"></div>

</body>

</html>


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI