Files
RSSHub/lib/routes/nowcoder/discuss.js
Carl Li f810f9ae97 修改牛客网帖子内容(description)解析方案 (#1688)
目前抓取的description数据,包含赞数和收藏数等,且会不断更新。这个 PR 把这些变化的数字去掉。

原先每个帖子都有

```html
收藏(43)</a></li><li class="oprt-item-share"><a class="oprt-item oprt-share js-share-oprt-item" href="javascript:void(0);">分享</a><div class="tooltip top js-share-tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"><span class="q-share-to"><a href="javascript:void(0);" class="js-share-link share-wb" data-share="weibo"></a><a href="javascript:void(0);" class="js-share-link share-tx" data-share="qzone"></a><a href="javascript:void(0);" class="js-share-wx share-wx"></a></span></div></div></li><li><a href="javascript:void(0);" class="js-post-like nc-req-auth oprt-item oprt-like">赞(0)
```

全都去掉了。
2019-03-06 12:10:34 +08:00

73 lines
2.1 KiB
JavaScript

const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const url = require('url');
const date = require('../../utils/date');
const host = 'https://www.nowcoder.com';
module.exports = async (ctx) => {
const type = ctx.params.type;
const order = ctx.params.order;
const link = `https://www.nowcoder.com/discuss?type=${type}&order=${order}`;
const response = await axios.get(link);
const $ = cheerio.load(response.data);
const type_name = $('a.discuss-tab.selected').text();
const order_name = $('li.selected a').text();
const list = $('li.clearfix')
.map(function() {
const info = {
title: $(this)
.find('div.discuss-main.clearfix a')
.text()
.trim()
.replace('\n', ' '),
link: $(this)
.find('div.discuss-main.clearfix a')
.attr('href'),
};
return info;
})
.get();
const out = await Promise.all(
list.map(async (info) => {
const title = info.title || 'tzgg';
const itemUrl = url.resolve(host, info.link);
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await axios.get(itemUrl);
const $ = cheerio.load(response.data);
const date_value = $('span.post-time')
.text()
.split(' ')[1];
const description = $('.nc-post-content')
.html()
.trim();
const single = {
title: title,
link: itemUrl,
description: description,
pubDate: date(date_value, 8),
};
ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60);
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `${type_name}${order_name}——牛客网讨论区`,
link: link,
item: out,
};
};