gpt4 book ai didi

javascript - node.js:雅虎天气的 jQuery 插件不打印数据

转载 作者:行者123 更新时间:2023-11-30 17:20:57 24 4
gpt4 key购买 nike

我让 node.js 使用 jQuery 和这个插件:http://simpleweatherjs.com/ .现在我想将天气数据用于另一项服务,而不是将其放入 HTML,但我无法访问/打印数据。错误函数有效,成功函数无效。

var jsdom = require("jsdom");
jsdom.env({
html: '<html><body><div id="weather"></div></body></html>',
scripts: [
'http://code.jquery.com/jquery-2.1.1.min.js',
'http://cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.0.2/jquery.simpleWeather.min.js'
],
done: function(errors, window) {
var $ = window.jQuery;

$.simpleWeather({
location: 'Paris',
woeid: '615702',
unit: 'c',
success: function(weather) {
console.log(weather.temp+'°'+weather.units.temp);
},
error: function(error) {
console.log(error.message);
}
});
}
});

这是正常情况下的样子 http://codepen.io/fleeting/pen/xwpar

插件也有一个 Node 模块,但我不知道如何让它工作,也没有文档。

最佳答案

我建议不要使用 jQuery 插件,而是直接使用底层数据。

Simpleweather 使用 yahoo API 来检索天气信息。

为了以易于使用的JSON格式获取天气信息,您可以使用yahoo的查询语言

<强>1。获取位置ID

例如,如果我想获取“瑞典斯德哥尔摩”的天气信息,我可以查询

SELECT woeid FROM geo.places where text="Stockholm, Sweden" LIMIT 1

这将返回 "woeid": "906057" [Execute YQL]

<强>2。获取天气信息

现在是时候使用我们在上一步中检索到的位置 ID 了。

SELECT item.condition.temp FROM weather.forecast WHERE woeid = 906057

这将返回 "temp": "81" [Execute YQL]

自动化

为了以编程方式接收 Node 中的温度,我建议使用 request模块。您可以从查询构建器页面复制 API 端点。在此示例中,它将是

https://query.yahooapis.com/v1/public/yql?q=SELECT%20item.condition.temp%20FROM%20weather.forecast%20WHERE%20woeid%20%3D%20906057&format=json

我猜你可以很容易地从这里开始,但为了完整起见,我将包含一个简单的 Node 示例:

var request = require('request');
var url = 'https://query.yahooapis.com/v1/public/yql?q=SELECT%20item.condition.temp%20FROM%20weather.forecast%20WHERE%20woeid%20%3D%20906057&format=json';

request(url, function (err, resp, body) {
if (err || resp.statusCode != 200)
return console.log('Could not get weather information');
var json = JSON.parse(body);
console.log('Temperature in Stockholm:', json.query.results.channel.item.condition.temp);
});

关于javascript - node.js:雅虎天气的 jQuery 插件不打印数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25140501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com