Node入门训练【5】【利用http请求抓取网页数据】

//get_data_from_web.js

var http = require('http');
var options = {
    hostname: 'www.oschina.net',
    port: 80,
    method: 'GET'
};

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);
});

req.end();

你可能感兴趣的:(Node.js)