feat: add 天眼查热门搜索 (#6837)

* feat: add 天眼查热门搜索

* docs: add anticrawler
This commit is contained in:
Ethan Shen 2021-02-04 10:42:44 +08:00 committed by GitHub
parent 2913df9d32
commit ecba021f17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 0 deletions

View File

@ -754,6 +754,12 @@ type 为 all 时category 参数不支持 cost 和 free
<Route author="kt286" example="/tprtc/news" path="/tprtc/news"/>
## 天眼查
### 热门搜索
<Route author="nczitzk" example="/tianyancha/hot" path="/tianyancha/hot" anticrawler="1"/>
## 无讼案例
### 案例

View File

@ -3913,4 +3913,7 @@ router.get('/gov/mohrss/sbjm/:category?', require('./routes/gov/mohrss/sbjm'));
router.get('/shinybbs/:id?', require('./routes/shinybbs/index'));
router.get('/shinybbs/latest', require('./routes/shinybbs/latest'));
// 天眼查
router.get('/tianyancha/hot', require('./routes/tianyancha/hot'));
module.exports = router;

View File

@ -0,0 +1,49 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const date = require('@/utils/date');
module.exports = async (ctx) => {
const rootUrl = 'https://www.tianyancha.com/';
const response = await got({
method: 'get',
url: rootUrl,
});
const $ = cheerio.load(response.data);
const list = $('.key')
.slice(0, 10)
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
};
})
.get();
const items = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.author = content('.resource').text().replace('来源:', '');
item.description = content('.news-content').html();
item.pubDate = date(content('.resource').next().text());
return item;
})
)
);
ctx.state.data = {
title: '热门搜索 - 天眼查',
link: rootUrl,
item: items,
};
};