突发奇想想在网上爬一本书,就轻易创立了个 js 文件开始写。
内容如下:
var axios = require('axios')
var cheerio = require('cheerio')
var fs = require('fs')
// 创立文件夹
if(!fs.existsSync('text')) {fs.mkdirSync('text')
}
// 请求书的地址
const baseUrl = 'https://www.shutxt.com/shaoer/18971'
const charpterFirst = 1064743
const charpterLast = 1064757
for(let i = charpterFirst; i < charpterLast + 1; i++) {axios.get(`https://www.shutxt.com/shaoer/18971/${i}.html`).then(res => {var $ = cheerio.load(res.data)
var title = $('title').text().split('_')[0]
var p = $('.zw p')
var content = ''
for (let i = 0; i < p.length; i++) {var li = p.eq(i).text()
content += li + '\r\n\r\n'
}
const article = title + '\r\n\r\n\r\n' + content
fs.writeFileSync(`./text/${title}.txt`, article, 'utf-8', (err) => {if(err)
console.log('写文件出错了,谬误是:' + err)
else
console.log(title + ':ok')
})
})
}
而后胜利的爬下来了一本书(十分傻瓜式),开心~