js weather 天气查询.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js weather 天气查询</title>
    <script src="https://pv.sohu.com/cityjson?ie=utf-8"></script>
    <script src="lib/jquery/jquery-3.2.1.js"></script>
</head>
<body>
<script>
    /*参考:https://www.cnblogs.com/hellowoeld/p/10687624.html*/
    function getTemperature(temperatureTxt) {
        /*根据温度文本信息,如:"高温 27℃","低温 17℃",获取温度,如:27℃, 17℃。*/
        return temperatureTxt.split(' ')[1]
    }

    function getWeather() {
        /*获取今天的天气信息。*/
        jQuery.support.cors = true;
        $.ajax({
            url: "http://wthrcdn.etouch.cn/weather_mini",
            type: "GET",
            dataType: 'json',
            data: {city: '天津'}, // 所在的市
            success: function (res) {
                console.log("res:", res);
                let city = res.data.city; // 所在城市
                let curTemperature = res.data.wendu + '℃';
                let highTemperatureTxt = res.data.forecast[0].high; // 最高温度
                let lowTemperatureTxt = res.data.forecast[0].low; // 最低温度
                let temperatureRange = getTemperature(lowTemperatureTxt) + '~' + getTemperature(highTemperatureTxt);
                let weatherType = res.data.forecast[0].type; // 天气状态
                console.log(city, curTemperature, temperatureRange, weatherType);
                // 天津 25℃ 17℃~27℃ 阴
                let fengxiang = res.data.forecast[0].fengxiang;
                let fengli = res.data.forecast[0].fengli.match(/\d+级/g)[0];
                console.log(fengxiang, fengli);
                // 南风 2级
            },
            error: function (err) {
                console.log(err)
            }
        });
    }

    getWeather();
</script>
</body>
</html>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个中国气象局的天气API接口,可以获取北京市的实时天气信息。 首先,我们需要在服务器端编写代码来获取天气数据并将其返回给客户端。以下是使用Node.js编写的示例代码: ```javascript const http = require('http'); const API_URL = 'http://www.weather.com.cn/data/sk/101010100.html'; http.createServer((req, res) => { if (req.url === '/weather') { http.get(API_URL, (response) => { let data = ''; response.on('data', (chunk) => { data += chunk; }); response.on('end', () => { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(data); }); }); } else { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('Not found'); } }).listen(3000, () => { console.log('Server running at http://localhost:3000'); }); ``` 该代码监听端口3000,并在访问`/weather`路径时获取天气数据并返回JSON格式的数据。 接下来,我们需要编写客户端代码来获取服务器返回的数据并显示在页面上。以下是使用jQuery编写的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>天气</title> </head> <body> <div id="weather"></div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(() => { $.getJSON('/weather', (data) => { const weather = data.weatherinfo; const html = ` <h1>${weather.city}</h1> <p>温度:${weather.temp}℃</p> <p>湿度:${weather.SD}</p> <p>风向:${weather.WD}</p> <p>风力:${weather.WS}</p> `; $('#weather').html(html); }); }); </script> </body> </html> ``` 该代码使用jQuery的`$.getJSON()`方法获取服务器返回的JSON格式数据,并将其显示在页面上。 最后,我们需要在命令行中启动服务器并访问页面。在命令行中执行以下命令: ```bash node app.js ``` 然后在浏览器中访问`http://localhost:3000`即可看到天气信息的页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值