我们用nodejs的http模块实现一个简单的爬虫:
什么事爬虫呢?就是我们获取到网页上面的一些数据信息,我们把它爬下来,爬到本地。比如说我们可以爬图片、爬html文档等。
下面来简单实现以下,如何去爬一个网页:
const https = require("https")
const fs = require("fs")
// 使用http的get方法,来爬去小滴课堂官网的数据 这里需要注意 我们爬取的是 https的网页 用的是https模块
https.get("https://xdclass.net/#/index",res => {
//设置一下编码格式
res.setEncoding(‘utf8‘);
// 创建一个html变量
let html = ‘‘;
// 监听response的data事件,将获取到的数据 保存在 html 变量中
res.on(‘data‘,chunk => {
html += chunk;
})
// 监听一下 响应结束的方法
res.on(‘end‘,()=>{
console.log(html);
// 用fs模块的writeFile方法,将网页内容 写入到index.txt文件中 这个方法会自动创建文件
fs.writeFile(‘./index.txt‘,html,(err)=>{
if(err) throw err;
console.log("写入成功");
})
})
})
。
原文:https://www.cnblogs.com/fqh123/p/13191833.html