NodeJS 获取网页源代码

时间:2024-03-08 11:05:32

获取网页源代码

node 获取网页源代码


  var http = require(\'http\');
  var url = "http://www.baidu.com/";
  // 参数url 和 回调函数
  http.get(url, function (res) {
    var html = \'\';
    // 绑定data事件 回调函数 累加html片段
    res.on(\'data\', function (data) {
      html += data;
    });

    res.on(\'end\', function () {
      console.log(html);
    });
  }).on(\'error\', function () {
    console.log(\'获取数据错误\');
  });

jquery 获取网页源代码

  $.get(window.location.href,function(res){
  console.log(res);
  });

原生 js 获取网页源代码

  // 这个获取到的是被解析过的 html, 并且没有 <!DOCTYPE html>
  document.documentElement.outerHTML