Pull specific line from HTTP request in node.js
NOOb here. I have an HTTP request that fetches all content from a specific web page. However, I need only a particular line: "Most recent instantaneous value: "
. In fact, I really need to store the value that follows value:
. Here is my code:
var http = require("http");
var options = {
host: 'waterdata.usgs.gov',
port: 80,
path: '/ga/nwis/uv?cb_72036=on&cb_00062=on&format=gif_default&period=1&site_no=02334400',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
I understand that I don't need all the statements console.log
, but I need to save console.log('BODY: ' + chunk);
so that all data loads?
source to share
Never do it the way I do in this quick example. There are many modules for DOM traversal, HTML / XML parsing, etc. They are much safer than a simple regular expression. But this is how you get the general idea:
var http = require("http");
var options = {
host: 'waterdata.usgs.gov',
port: 80,
path: '/ga/nwis/uv?cb_72036=on&cb_00062=on&format=gif_default&period=1&site_no=02334400',
};
function extract (body, cb) {
if(!body)
return;
var matches=body.match(/Most recent instantaneous value: ([^ ]+) /);
if(matches)
cb(matches[1]);
}
http.get(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
extract(chunk, function(v){ console.log(v); });
});
}).on('error', function(e) {
console.log('problem with request: ' + e.message);
});
Somehow I also got a different page when posting a POST instead of a GET request. So I changed this bit ...
Regarding your second question: No, you don't need to save any of the operators console.log()
. Just use callbacks and you're good to go! :-)
source to share