From bed9cd90fbb6970d5af099d072ee9fa890d7df35 Mon Sep 17 00:00:00 2001 From: Lince Date: Tue, 7 Aug 2018 17:02:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=8F=98=E8=B5=B7=E7=82=B9=E7=AB=A0?= =?UTF-8?q?=E8=8A=82=E7=9A=84=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F=20(#422)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复了部分小说无法抓取的bug #420 --- routes/qidian/chapter.js | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/routes/qidian/chapter.js b/routes/qidian/chapter.js index ea3c3d7db1..780a10f987 100644 --- a/routes/qidian/chapter.js +++ b/routes/qidian/chapter.js @@ -10,27 +10,36 @@ module.exports = async (ctx) => { url: `https://book.qidian.com/info/${id}#Catalog`, headers: { 'User-Agent': config.ua, - Referer: `https://book.qidian.com/info/${id}`, }, }); - const $ = cheerio.load(response.data); const name = $('.book-info>h1>em').text(); - const list = $('#j-catalogWrap li a'); - const cover_url = $('.bookImg>img').attr('src'); + const cover_url = 'https:' + $('#bookImg>img').attr('src'); + + const csrfToken = response.headers['set-cookie'].find((s) => s.startsWith('_csrfToken=')).split(';')[0]; + + const chapters_response = await axios({ + method: 'get', + url: `https://book.qidian.com/ajax/book/category?${csrfToken}&bookId=${id}`, + headers: { + 'User-Agent': config.ua, + }, + }); const chapter_item = []; - for (let i = list.length - 1; i >= 0; i--) { - const el = $(list[i]); - const item = { - title: el.text(), - link: `https:${el.attr('href')}`, - pubDate: new Date(el.attr('title').substring(5, 25)).toUTCString(), - description: el.attr('title'), - }; - chapter_item.push(item); + for (let i = 0; i < chapters_response.data.data.vs.length; i++) { + const chapters = chapters_response.data.data.vs[i].cs; + for (let j = 0; j < chapters.length; j++) { + const chapter = chapters[j]; + chapter_item.push({ + title: chapter.cN, + pubDate: new Date(chapter.uT).toUTCString(), + link: `https://vipreader.qidian.com/chapter/${id}/${chapter.id}`, + }); + } } + ctx.state.data = { title: `起点 ${name}`, link: `https://book.qidian.com/info/${id}`,