下面的代码熄灭并从代码中的每个键的URL中获取JavaScript变量,然后一旦成功检索到所有变量,它就会从累积结果中创建一个JSON对象。这在客户端很有用,但我想每15分钟在服务器端进行一次处理(对于更多的密钥代码)。我当然可以用Python修补一些东西来做这件事,但考虑到JS中的代码非常简单,这看起来有点荒谬。我对node.js不太熟悉,但在阅读之后我认为这可能是最好的解决方案 - 尽管我确定还有其他选择。我将如何排队脚本检索,以便在完成后收到消息并将JSON文件发送到服务器上的存储位置?可以在服务器端使用类似nodeFile.js?
之类的东西来完成var codes = {'C': {}, 'S': {}, 'W': {}};
var keys = [];
for (var k in codes) keys.push(k);
var queue = keys.map(function (d) {
var url = "http://www.agricharts.com/marketdata/jsquote.php?user=&pass=&allsymbols=" + d;
return $.getScript(url)
.done(function (e, textStatus) {
codes[d] = qb; // qb is the name of the JS variable found in the URL
});
});
$.when.apply(null, queue).done(function () {
var output = JSON.stringify(codes); // save this JSON file to the server for processing on the client side
});
答案 0 :(得分:1)
你可以做一个http get:
require('http');
var codes = {'C': {}, 'S': {}, 'W': {}};
var keys = [];
for (var k in codes) keys.push(k);
var counter = 0;
keys.map(function(d){
var url = "http://www.agricharts.com/marketdata/jsquote.php?user=&pass=&allsymbols=" + d;
http.get(url, function(res) {
var scriptData = '';
res.on('data', function (chunk) {
scriptData+=chunk;
});
res.on('end',function(){
counter++;
eval(scriptData);
codes[d] = qb;
if(counter==keys.length){
var output = JSON.stringify(codes); // save this JSON file to the server for processing on the client side
}
});
});
});
之后,您可以排队并将结果发布到服务器。退房:http://nodejs.org/api/http.html#http_http_get_options_callback
V8 VM实现了ECMAScript规范,因此除了DOM操作之外,你可以在浏览器中做很多事情,可以在像eval
函数这样的nodejs中完成