fix: neunews (#4018)

This commit is contained in:
Jeason Lau
2020-02-18 18:54:18 +08:00
committed by GitHub
parent 93cc83997b
commit 04804d588e
+42 -41
View File
@@ -16,48 +16,49 @@ module.exports = async (ctx) => {
const title = $('title').text();
const items = $('.column-news-list > .news_list > .news > .news_title')
.slice(0, 10)
.children();
const results = [];
items.each(async (index, item) => {
const label = $(item);
const title = $(label).attr('title');
const url = baseUrl + $(label).attr('href');
const description = await ctx.cache.tryGet(url, async () => {
const result = await got.get(url);
const $ = cheerio.load(result.data);
// 处理部分格式
$('article')
.find('span')
.each(function() {
const temp = $(this).text();
$(this).replaceWith(temp);
});
$('article')
.find('div')
.each(function() {
const temp = $(this).html();
$(this).replaceWith(temp);
});
$('article')
.find('a')
.remove();
$('article')
.find('p')
.each(function() {
$(this).removeAttr('style');
$(this).removeAttr('class');
});
return $('article').html();
});
const result = {
title: title,
description: description,
link: url,
};
results.push(result);
});
.get();
const results = await Promise.all(
items.map(async (item) => {
const $ = cheerio.load(item);
const title = $('a').attr('title');
const url = baseUrl + $('a').attr('href');
const description = await ctx.cache.tryGet(url, async () => {
const result = await got.get(url);
const $ = cheerio.load(result.data);
// 处理部分格式
$('article')
.find('span')
.each(function() {
const temp = $(this).text();
$(this).replaceWith(temp);
});
$('article')
.find('div')
.each(function() {
const temp = $(this).html();
$(this).replaceWith(temp);
});
$('article')
.find('a')
.remove();
$('article')
.find('p')
.each(function() {
$(this).removeAttr('style');
$(this).removeAttr('class');
});
return $('article').html();
});
const result = {
title: title,
description: description,
link: url,
};
Promise.resolve(result);
})
);
ctx.state.data = {
title: title,
title: `东北大学新闻网-${title}`,
item: results,
};
};