diff --git a/lib/v2/cna/maintainer.js b/lib/v2/cna/maintainer.js index 77e7f46e2..25c21691d 100644 --- a/lib/v2/cna/maintainer.js +++ b/lib/v2/cna/maintainer.js @@ -1,3 +1,4 @@ module.exports = { '/:id?': ['nczitzk'], + '/web/:id?': ['dzx-dzx'], }; diff --git a/lib/v2/cna/radar.js b/lib/v2/cna/radar.js index 92cec6d1b..5431d1dc9 100644 --- a/lib/v2/cna/radar.js +++ b/lib/v2/cna/radar.js @@ -8,6 +8,12 @@ module.exports = { source: ['/list/:id', '/topic/newstopic/:id'], target: (params) => `/cna/${params.id.replace('.aspx', '')}`, }, + { + title: '分类(网页爬虫方法)', + docs: 'https://docs.rsshub.app/routes/traditional-media#zhong-yang-tong-xun-she', + source: ['/list/:id'], + target: (params) => `/cna/web/${params.id.replace('.aspx', '')}`, + }, ], }, }; diff --git a/lib/v2/cna/router.js b/lib/v2/cna/router.js index a5d65c468..a8cb0cb48 100644 --- a/lib/v2/cna/router.js +++ b/lib/v2/cna/router.js @@ -1,3 +1,4 @@ module.exports = (router) => { router.get('/:id?', require('./')); + router.get('/web/:id?', require('./web/')); }; diff --git a/lib/v2/cna/web/index.js b/lib/v2/cna/web/index.js new file mode 100644 index 000000000..e050d672b --- /dev/null +++ b/lib/v2/cna/web/index.js @@ -0,0 +1,62 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +module.exports = async (ctx) => { + const id = ctx.params.id || 'aall'; + + let rootUrl; + + if (/^\d+$/.test(id)) { + rootUrl = `https://www.cna.com.tw/topic/newstopic/${id}.aspx`; + } else { + rootUrl = `https://www.cna.com.tw/list/${id}.aspx`; + } + const response = await got({ + method: 'get', + url: rootUrl, + }); + + const $ = cheerio.load(response.data); + const list = $('*:is(.pcBox .caItem, .mainList li a div) h2') + .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 10) + .toArray() + .map((item) => { + item = $(item); + return { + title: item.text(), + link: item.parents('a').attr('href'), + pubDate: timezone(parseDate(item.next().text()), +8), + }; + }); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + const topImage = content('.fullPic').html(); + + item.description = (topImage === null ? '' : topImage) + content('.paragraph').eq(0).html(); + item.category = [ + ...content("meta[property='article:tag']") + .get() + .map((e) => e.attribs.content), + content('.active > a').text(), + ]; + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: rootUrl, + item: items, + }; +}; diff --git a/website/docs/routes/traditional-media.mdx b/website/docs/routes/traditional-media.mdx index 8b3326323..cdf4d78c5 100644 --- a/website/docs/routes/traditional-media.mdx +++ b/website/docs/routes/traditional-media.mdx @@ -2831,6 +2831,10 @@ category 对应的关键词有 +### 分类(网页爬虫方法) {#zhong-yang-tong-xun-she-fen-lei-wang-ye-pa-chong-fang-fa} + + + ## 组织人事报 {#zu-zhi-ren-shi-bao} ### 电子报 {#zu-zhi-ren-shi-bao-dian-zi-bao}