获取网页源代码
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
![](https://image.shishitao.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2Jhbm5lci8yMDIxL3ZvbGNlbmdpbmUtYzIuanBn.jpg?w=700&webp=1)