var xpath=require("xpath");
var fs=require("fs");
var dom = require('xmldom').DOMParser;
var http = require('http');
var opt = {
path:'https://www.google.co.jp',//这里是访问的路径
headers:{
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
}
}
var req=http.request(opt,function(res){
res.setEncoding("utf-8");
var html ='';
res.on("data",function(chunk){
html += chunk;
console.log(chunk.toString())
});
res.on('end',function(){
fileWrite('2.html',html);
});
console.log(res.statusCode);
});
req.on("error",function(err){
console.log(err.message);
});
req.end();
function fileWrite(filename,body)
{
fs.writeFile(filename, body, function (err) {
if (err) throw err;
console.log(filename+'Saved successfully'); //文件被保存
});
}