var http = require('http');
var url = "http://pamodata.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('获取数据错误');
});
- 第一步引入http。
- 第二步引入url。
- 第三步发起http.get()请求,参数url和回调函数,回调函数中加入形参res。
- 绑定data事件,执行代码片段累加。
本文介绍如何利用Node.js中的http模块抓取指定URL的内容。通过示例代码展示了从请求到解析HTML的基本流程,包括错误处理。
574

被折叠的 条评论
为什么被折叠?



