From 9ff227f47151f2c60ddc43f8f595d207b1611fe3 Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Mon, 15 Jun 2020 04:38:30 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=BE=8E=E6=B9=83=20DOM=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=20(#4992)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/thepaper/channel.js | 2 +- lib/routes/thepaper/utils.js | 27 ++++++++++++--------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/routes/thepaper/channel.js b/lib/routes/thepaper/channel.js index d330bac32..4993ecac1 100644 --- a/lib/routes/thepaper/channel.js +++ b/lib/routes/thepaper/channel.js @@ -2,7 +2,7 @@ const utils = require('./utils'); module.exports = async (ctx) => { const { id } = ctx.params; - const link = `https://www.thepaper.cn/channel_${id}`; + const link = `https://m.thepaper.cn/channel_${id}`; const items = await utils.ProcessFeed(link, ctx); ctx.state.data = { diff --git a/lib/routes/thepaper/utils.js b/lib/routes/thepaper/utils.js index d77bda700..5b991a389 100644 --- a/lib/routes/thepaper/utils.js +++ b/lib/routes/thepaper/utils.js @@ -1,5 +1,5 @@ const cheerio = require('cheerio'); -const dayjs = require('dayjs'); +const date = require('@/utils/date'); const got = require('@/utils/got'); module.exports = { @@ -7,32 +7,29 @@ module.exports = { const res = await got.get(link); const $ = cheerio.load(res.data); - const list = $('#mainContent .news_li').slice(0, 10).get(); + const list = $('.t_news').slice(0, 10).get(); return await Promise.all( list.map(async (item) => { const $ = cheerio.load(item); - const itemUrlID = $(item).find('.news_tu a').attr('href'); - const itemUrl = `https://www.thepaper.cn/${itemUrlID}`; + const itemUrlID = $(item).find('.news_tit02 a').attr('href'); + const itemUrl = `https://m.thepaper.cn/${itemUrlID}`; const cache = await ctx.cache.get(itemUrl); if (cache) { return Promise.resolve(JSON.parse(cache)); } + const res = await got.get(itemUrl); const content = cheerio.load(res.data); - const serverOffset = new Date().getTimezoneOffset() / 60; - const author = content('.newscontent .news_about p').slice(0, 1).text(); - content('.newscontent .news_about p span').remove(); - const pubDateStr = content('.newscontent .news_about p').slice(1, 2).text().trim(); + + content('.contheight').replaceWith('
'); + const single = { - title: content('.newscontent .news_title').text(), - guid: itemUrl, + title: content('h1.t_newsinfo').text(), link: itemUrl, - description: content('.newscontent .news_txt').html(), - pubDate: dayjs(pubDateStr) - .add(-8 - serverOffset, 'hour') - .toISOString(), - author: author, + description: $(content('.news_part_limit div')[0]).html(), + pubDate: date($(content('.about_news')[1]).text().trim()), + author: $(content('.about_news')[0]).text(), }; return Promise.resolve(single); })