fix: zhaishuyuan content incomplete (#6877) (#6878)

* fix: zhaishuyuan content incomplete (#6877)

* fix: remove unused var

* [CodeFactor] Apply fixes to commit c8e441a

[ci skip] [skip ci]

Co-authored-by: codefactor-io <support@codefactor.io>
This commit is contained in:
Ving 2021-02-14 02:19:26 +08:00 committed by GitHub
parent 771d9d6356
commit fd776393b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 4 deletions

View File

@ -6,7 +6,7 @@ module.exports = async (ctx) => {
const id = ctx.params.id;
const url = 'https://www.zhaishuyuan.com';
const link = `${url}/book/${id}`;
let extendedGot = got.extend({ headers: { Referer: url }, responseType: 'buffer' });
const extendedGot = got.extend({ headers: { Referer: url }, responseType: 'buffer' });
const response = await extendedGot.get(link);
const html = iconv.decode(response.data, 'gb2312');
const $ = cheerio.load(html);
@ -17,13 +17,16 @@ module.exports = async (ctx) => {
.find('a')
.map((_, { attribs: { title, href } }) => ({ title, link: `${url}${href}` }))
.get();
extendedGot = got.extend({ headers: { Referer: link }, responseType: 'buffer' });
const item = await Promise.all(
list.map(
async ({ title, link }) =>
await ctx.cache.tryGet(link, async () => {
const response = await extendedGot.get(link);
const html = iconv.decode(response.data, 'gb2312');
const browser = await require('@/utils/puppeteer')();
const page = await browser.newPage();
await page.goto(link);
const encoded = await page.$('#content > div > a');
await encoded.evaluate((encoded) => encoded.click());
const html = await page.evaluate(() => document.documentElement.innerHTML);
const $ = cheerio.load(html);
const content = $('#content');
content.find('div').remove();