涉及三个模块:request、cheerio、fs
request用于发送http请求
cheerio用于操作DOM相关
fs文件系统
// 爬取网站小说
let request = require('request');
let cheerio = require('cheerio');
let fs = require('fs');
// fs.writeFile('demo.txt', 'aaaaa', (err) => {
// if (err) console.log(err);
// console.log('write success')
// })
request('http://book.zongheng.com/showchapter/1159968.html', (err, result) => {
if (err) console.log(err);
let $ = cheerio.load(result.body);
$('ul[class^="chapter-list"] li a').each((index, el) => {
const href = $(el).attr('href');
const title = $(el).text();
request(href, (error, res) => {
let html = cheerio.load(res.body);
html('div[class^="content"]').each((idx, element) => {
fs.writeFile(`${index + title}.txt`, html(element).text(), 'utf8', (e, result) => {
if (e) console.log(e);
console.log(222);
})
})
})
})
})