40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
const got = require('@/utils/got');
|
|
const cheerio = require('cheerio');
|
|
|
|
module.exports = async (ctx) => {
|
|
const url = 'http://www.glo.com.cn/insights/prespectives/';
|
|
const res = await got.get(url);
|
|
const $ = cheerio.load(res.data);
|
|
const list = $('.news_listul li').get();
|
|
|
|
const out = await Promise.all(
|
|
list.map(async (item) => {
|
|
const $ = cheerio.load(item);
|
|
const title = $('a .newstit').text();
|
|
const itemUrl = 'http://www.glo.com.cn/' + $('a').attr('href');
|
|
|
|
const cache = await ctx.cache.get(itemUrl);
|
|
if (cache) {
|
|
return Promise.resolve(JSON.parse(cache));
|
|
}
|
|
|
|
const responses = await got.get(itemUrl);
|
|
const $d = cheerio.load(responses.data);
|
|
|
|
const single = {
|
|
title,
|
|
link: itemUrl,
|
|
description: $d('.editor_con').html(),
|
|
};
|
|
|
|
ctx.cache.set(itemUrl, JSON.stringify(single));
|
|
return Promise.resolve(single);
|
|
})
|
|
);
|
|
ctx.state.data = {
|
|
title: $('title').text(),
|
|
link: url,
|
|
item: out,
|
|
};
|
|
};
|