GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — readBook_node/app.js at master · feverdestiny/readBook_node Expand file tree/ Copy pathapp.jsMore file actions 123 lines (119 loc) · 3.14 KB / Copy pathapp.jsFile metadata and controls123 lines (119 loc) · 3.14 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 /** * Created by feverdestiny on 2017/9/22. */ const cheerio = require("cheerio"); const request = require("request"); const fs = require("fs"); const path = require("path"); let count = 0; //叠加 let url = 'http://www.23us.cc/html/150/150567/'; //小说Url let list = []; //章节List let booksName = ''; //小说名称 /** * 获取小说目录页 */ const books = function () { request(url, function (err, res, body) { if (!err && res.statusCode == 200) { console.log(`获取小说基本信息成功·······`) booksQuery(body); } else { console.log('err:' + err) } }) } /** * 处理小说名称及其小说目录 * @param {*} body */ const booksQuery = function (body) { $ = cheerio.load(body); booksName = $('.btitle').find('h1').text(); //小说名称 $('.chapterlist').find('a').each(function (i, e) { //获取章节UrlList list.push($(e).attr('href')) }); createFolder(path.join(__dirname, `/book/${booksName}.txt`)); //创建文件夹 fs.createWriteStream(path.join(__dirname, `/book/${booksName}.txt`)) //创建txt文件 console.log(`开始写入《${booksName}》·······`) getBody(); //获取章节信息 } /** * 获取章节页面信息 * */ const getBody = function () { let primUrl = url + list[count]; // console.log(primUrl) request(primUrl, function (err, res, body) { if (!err && res.statusCode == 200) { toQuery(body); } else { console.log('err:' + err) } }) }; /** * 处理章节页面信息 * @param {any} body */ const toQuery = function (body) { $ = cheerio.load(body); const title = $('h1').text(); //获取章节标题 const content = Trim($('#content').text(), 'g'); //获取当前章节文本内容并去除文本所有空格 writeFs(title, content); } /** * 写入txt文件 * @param {*} title * @param {*} content */ const writeFs = function (title, content) { // 添加数据 fs.appendFile(path.join(__dirname, `/book/${booksName}.txt`), title, function (err) { if (err) throw err; }); fs.appendFile(path.join(__dirname, `/book/${booksName}.txt`), content, function (err) { if (err) { console.log(err) } else { console.log(title + '········保存成功') if (count + 1 < list.length) { //当前页码是否超过章节数 count = count + 1; getBody(); } } }); } /** * 创建文件夹 * * @param {any} to */ const createFolder = function (to) { //文件写入 var sep = path.sep var folders = path.dirname(to).split(sep); var p = ''; while (folders.length) { p += folders.shift() + sep; if (!fs.existsSync(p)) { fs.mkdirSync(p); } } }; /** * * 去除所有空格 * @param {any} str * @param {any} is_global * @returns */ const Trim = function (str, is_global) { var result; result = str.replace(/(^\s+)|(\s+$)/g, ""); if (is_global.toLowerCase() == "g") { result = result.replace(/\s/g, ""); } return result; } books(); |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |