From e1787758c8dc53cecaee87d96c8448fceea3916f Mon Sep 17 00:00:00 2001 From: Fan Shen <1370275510@qq.com> Date: Thu, 13 Jun 2019 15:25:44 +0800 Subject: [PATCH] fix: link bug and add docs keywords (#2398) --- docs/other.md | 2 +- lib/routes/fx678/kx.js | 54 +++++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/docs/other.md b/docs/other.md index 667fadd6c..d46341893 100644 --- a/docs/other.md +++ b/docs/other.md @@ -16,7 +16,7 @@ pageClass: routes -## 7x24 小时快讯 +## 汇通网 ### 7x24 小时快讯 diff --git a/lib/routes/fx678/kx.js b/lib/routes/fx678/kx.js index 747317800..7cd0c6767 100644 --- a/lib/routes/fx678/kx.js +++ b/lib/routes/fx678/kx.js @@ -1,34 +1,44 @@ const cheerio = require('cheerio'); const got = require('@/utils/got'); +const url = require('url'); +const host = 'https://kx.fx678.com/'; module.exports = async (ctx) => { - const url = 'https://kx.fx678.com/'; - const res = await got.get(url); + const link = 'https://kx.fx678.com/'; + const res = await got.get(link); const $ = cheerio.load(res.data); - - const list = $('.body_zb ul .body_zb_li'); - // 爬取页面的前30条消息 - const content = list + // 页面新闻消息列表 + const list = $('.body_zb ul .body_zb_li .zb_word') + .find('.list_font_pic > a:first-child') + .map((i, e) => $(e).attr('href')) .slice(0, 30) - .map(function() { - const title = $(this) - .find('.zb_time') - .text(); - const description = $(this) - .find('.zb_word') - .text() - .replace(/(^\s*)|(\s*$)/g, ''); - const item = { - title, - description, - }; - return item; - }) .get(); + const out = await Promise.all( + list.map(async (itemUrl) => { + const absoluteUrl = url.resolve(host, itemUrl); + const cache = await ctx.cache.get(absoluteUrl); + // 判断缓存 + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const res = await got.get(absoluteUrl); + const $ = cheerio.load(res.data); + + const item = { + title: $('.article-cont h1').text(), + link: absoluteUrl, + description: $('.article-main .content').html(), + pubDate: new Date().toUTCString(), + }; + ctx.cache.set(absoluteUrl, JSON.stringify(item)); + + return Promise.resolve(item); + }) + ); ctx.state.data = { title: '7x24小时快讯', - link: url, - item: content, + link: link, + item: out, }; };