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'); //文件被保存
  });
}

 

相关文章:

  • 2021-11-20
  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-20
  • 2021-11-29
  • 2021-10-31
  • 2021-12-18
猜你喜欢
  • 2021-11-19
  • 2021-04-14
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案