Javascript 使用node.js中HTTP请求的数据
我的任务是从中获取数据并将其拆分/排序为有用的内容。首先,我尝试将数据划分为多个类别,以便稍后在chart.js或其他文件中使用,但当我尝试打印字段时,它只显示为[]Javascript 使用node.js中HTTP请求的数据,javascript,node.js,httprequest,Javascript,Node.js,Httprequest,我的任务是从中获取数据并将其拆分/排序为有用的内容。首先,我尝试将数据划分为多个类别,以便稍后在chart.js或其他文件中使用,但当我尝试打印字段时,它只显示为[] var options = { host: 'services.swpc.noaa.gov', path: '/text/ace-swepam.txt', port: 80, method: 'POST' }; var req = http.request(options, funct
var options = {
host: 'services.swpc.noaa.gov',
path: '/text/ace-swepam.txt',
port: 80,
method: 'POST'
};
var req = http.request(options, function (res) {
res.on('data', function (chunk) {
// console.log('BODY: ' + chunk);
results += chunk.toString();
//split results into an array by each new line
lines = results.split("\n");
//delete header lines
lines.splice(0, 18);
if (lines.length <= 20) {
return;
}
console.log(lines);
});
res.on('end', function (e) {
callback();
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
function callback() {
for (var line in lines) {
var x = [];
x = lines[line].split(" ");
var statuscode = x[14];
if (statuscode == 0) {
if (lines[line].indexOf('-') === -1) {
year.push(x[0]);
month.push(x[1]);
day.push(x[2]);
time.push(x[4]);
statusno.push(statuscode);
proton.push(x[22]);
bulksp.push(x[28]);
iontemp.push(x[33]);
}
}
}
// console.log(year, month, day, time, statusno, proton, bulksp, iontemp)
}
var选项={
主持人:“services.swpc.noaa.gov”,
路径:'/text/ace swepam.txt',
港口:80,
方法:“发布”
};
var req=http.request(选项、函数(res){
res.on('data',函数(块){
//log('BODY:'+chunk);
结果+=chunk.toString();
//按每一新行将结果拆分为一个数组
行=结果。拆分(“\n”);
//删除标题行
线。拼接(0,18);
如果(lines.length数据行似乎不是以制表符分隔的。我希望它们是固定长度的
这是我收到的第一行。
“2015 08 18 1708 57252 61680 0 0 2.6 45”
而不是尝试按选项卡拆分此行
fields = line.split("\t");
创建每个字段长度的数组,并使用substring方法将其拆分
下面是解析返回数据的完整代码。它给出了119行或6-7行的状态!=0(因此被跳过)。您的变量每个都有112个条目
res.on('data', function (chunk) {
var fieldLengths = [0, 4, 7, 10, 16, 24, 32, 37, 48, 59, 72];
// console.log('BODY: ' + chunk);
results += chunk.toString();
//split results into an array by each new line
lines = results.split("\n");
// for me, the first "chunk" is incomplete. Throw it away and just use the second chunk.
if (lines.length <= 20) {
return;
}
//delete header lines
lines.splice(0, 18);
for (var line in lines) {
console.log("entry: " + lines[line]);
//split into data fields
var lineText = lines[line];
var fields = [];
for (var i = 0; i <= fieldLengths.length -1; i++) {
fields.push(lineText.substring(fieldLengths[i], fieldLengths[i + 1]));
}
//if there are no problems (status code 0)
//add the data to their respective fields
if (fields[6] == 0) {
year.push(fields[0]);
month.push(fields[1]);
day.push(fields[2]);
time.push(fields[3]);
statusno.push(fields[6]);
proton.push(fields[7]);
bulksp.push(fields[8]);
iontemp.push(fields[9]);
}
}
});
res.on('end', function (e) {
console.log(year);
});
res.on('data',函数(块){
var FieldLength=[0,4,7,10,16,24,32,37,48,59,72];
//log('BODY:'+chunk);
结果+=chunk.toString();
//按每一新行将结果拆分为一个数组
行=结果。拆分(“\n”);
//对我来说,第一个“块”是不完整的。扔掉它,只使用第二个块。
如果(行数、长度)
或
只需将此函数放入res.on('end')。
我不是100%确定你到底在做什么,希望这有帮助
编辑:
谢谢你,你这个了不起的人,你:)当然!如果对你有用,你能把这个作为答案吗?对不起,是的,我从来没有使用过这个网站,所以我不知道我在做什么。我问过下面的人,但我也会问你-我已经用了子字符串方法,这是很好的,但它只是循环通过前30个左右(1725-1824对我来说)记录三次,而不是一次获取所有数据。有什么明显的原因吗?我现在获取了正确的数据。请稍候,我将发布解析代码。尝试直接从网站下载数据。它只是一个文本文件。使用脚本中的URL。然后,像t一样正确地进行解析帽子。这可能是因为他们的服务有点奇怪。如果你能确定你的团队工作正常,你可以和他们谈谈。谢谢!:)我用子串方法工作,这很好,但它只是循环通过前30个左右(我是1725-1824)记录三次,而不是一次获取所有数据。有什么明显的原因吗?我将用解决方案的代码编辑我的答案。该解决方案在站点上的所有数据行中循环,似乎正在更新。从站点获取所有数据并将其解析为x。您可以打印x并查看您的值所在的索引(所有行都是常量,因为间距相同)。我一直这样做直到时间值,因为这是第一个实际不同的值。好的,我使用了上面提到的子字符串方法,但是你的方法似乎更简单,所以我已经复制了一个运行你的方法的副本。但是我仍然没有得到最近一个小时左右的数据:/
function callback() {
for (var line in lines) {
//split into data fields
year.push(lines[line].substring(x,y));//fill in x and y
//month.push..
//day.push..
}
}
callback() {
var x = [];
for (var line in lines){
x = lines[line].split(" ");
console.log(x);
year.push(x[index]) // index being where year was split into x
}
}
var options = {
host: 'services.swpc.noaa.gov',
path: '/text/ace-swepam.txt',
port: 80,
method: 'POST'
};
var req = http.request(options, function (res) {
res.on('data', function (chunk) {
// console.log('BODY: ' + chunk);
results += chunk.toString();
//split results into an array by each new line
lines = results.split("\n");
//delete header lines
lines.splice(0, 18);
});
res.on('end', function (e) {
callback();
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
function callback() {
for (var line in lines) {
var x = [];
x = lines[line].split(" ");
//console.log(x); Print x and see which index of x has the vaule you want. Constant for all
year.push(x[0]);
month.push(x[1]);
day.push(x[2]);
time.push(x[4]);
}
//console.log(year,month,day,time); Check final result
}