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
| const axios = require('axios'); const cheerio = require('cheerio'); const Koa = require('koa'); const path = require('path'); const serve = require('koa-static'); const Router = require('koa-router'); const bodyParser = require('koa-bodyparser'); const iconv = require('iconv-lite'); const https = require('https'); const app = new Koa(); const router = new Router();
const staticPath = path.join(__dirname, 'public'); app.use(serve(staticPath)); app.use(bodyParser());
router.post('/getPages', async (ctx) => { const body = ctx.request.body; if (body && body.url) { const headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0', 'cookie':"cf_chl_3=37be84a8cd3c263;cf_clearance=6ttfCy6FiTwm.O.u_kvRO8fYwGCk63azSaZBhbp6y50-1710649771-1.0.1.1-x6tbLBsxcDRpH6c12E1UEMMD9GLIPNiNevlcazAjNgFRQEL0IMHpSL_cTVzNoFDX1VR6aq78iGfQt.dTnUW8rg;" }; await axios.get(body.url, { headers: headers, responseType: 'arraybuffer' }).then(response => { const html = iconv.decode(response.data, 'gb2312'); const $ = cheerio.load(html); let next = "" let prev = "" let content = "" let title = "" title = $('.chaptertitle').html() for(let i = 0;i < $('#content p').length;i++){ if(!($('#content p').eq(i).attr('style')|| $('#content p').eq(i).attr('class'))){ content += $('#content p').eq(i).html() } } prev = `https://www.feisxs.com/Html/44069/${$('#prev').attr('href')}` next = `https://www.feisxs.com/Html/44069/${$('#next').attr('href')}` ctx.body = { title, content, next, prev } }).catch((error) => { ctx.status = 300; ctx.body = error; }); } else { ctx.status = 400; ctx.body = '获取数据出错'; } });
app.use(router.routes());
app.use((ctx) => { ctx.status = 400; ctx.body = 'Bad Request'; });
app.use(router.allowedMethods());
const port = 3000; app.listen(port, () => { console.log(`应用运行在 http://localhost:${port}`); });
|