NodeJS 獲取網頁源代碼


獲取網頁源代碼

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM