fix: 修复苏州市政府 (#5358)
This commit is contained in:
parent
59a1c0198e
commit
020cef5e5b
|
|
@ -1,8 +1,8 @@
|
|||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
|
||||
const util = require('./utils');
|
||||
module.exports = async (ctx) => {
|
||||
const link = 'http://www.suzhou.gov.cn/szxxgk/front/xxgk_right.jsp?sitecode=szsrmzf';
|
||||
const link = 'http://www.suzhou.gov.cn/szxxgk/front/xxgk_right.jsp';
|
||||
const response = await got.get(link);
|
||||
const data = response.data;
|
||||
const $ = cheerio.load(data);
|
||||
|
|
@ -21,13 +21,15 @@ module.exports = async (ctx) => {
|
|||
const arr = await ctx.cache.tryGet(contentUrl, async () => {
|
||||
const fullText = await got.get(contentUrl);
|
||||
const fullTextData = cheerio.load(fullText.data);
|
||||
const description = fullTextData('.content').html();
|
||||
const title = fullTextData('.title.bold').text().replace(/\s*/g, '');
|
||||
return new Array(title, description);
|
||||
const content = util.content(fullText.data);
|
||||
const title = fullTextData('h1').text().replace(/\s*/g, '');
|
||||
|
||||
return new Array(title, content);
|
||||
});
|
||||
return {
|
||||
title: arr[0],
|
||||
description: arr[1],
|
||||
pubDate: item.find('td:nth-child(4)').text(),
|
||||
link: contentUrl,
|
||||
};
|
||||
})
|
||||
|
|
|
|||
|
|
@ -2,30 +2,35 @@ const got = require('@/utils/got');
|
|||
const cheerio = require('cheerio');
|
||||
const logger = require('@/utils/logger');
|
||||
const liburl = require('url');
|
||||
|
||||
const util = require('./utils');
|
||||
const root_url = 'http://www.suzhou.gov.cn/';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const uid = ctx.params.uid;
|
||||
let url = '';
|
||||
let title = '';
|
||||
let urljs = '';
|
||||
switch (uid) {
|
||||
case 'qxkx':
|
||||
case 'district':
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/qxkx/nav_list.shtml';
|
||||
urljs = 'http://www.suzhou.gov.cn/szinf/info/getInfoCommon/?pagesize=15&currpage=1&channelid=75c636ea0efb487ea7e479e3cc0ff3e5';
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/zwggqxkx/nav_list_js.shtml';
|
||||
title = '苏州市政府 - 区县快讯';
|
||||
break;
|
||||
case 'szyw':
|
||||
case 'news':
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/szyw/nav_list.shtml';
|
||||
urljs = 'http://www.suzhou.gov.cn/szinf/info/getInfoCommon/?pagesize=15&currpage=1&channelid=5057aeffb1a84a7e8aeded87728da48c';
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/szyw/nav_list_js.shtml';
|
||||
title = '苏州市政府 - 政务要闻';
|
||||
break;
|
||||
case 'bmdt':
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/bmdt/nav_list.shtml';
|
||||
urljs = 'http://www.suzhou.gov.cn/szinf/info/getInfoCommon/?pagesize=15&currpage=1&channelid=b3d097e3eb79421f88439ea381ce33c3';
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/bmdt/nav_list_js.shtml';
|
||||
title = '苏州市政府 - 部门动态';
|
||||
break;
|
||||
case 'xwsp':
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/xwsp/nav_list.shtml';
|
||||
urljs = 'http://www.suzhou.gov.cn/szinf/info/getInfoCommon/?pagesize=15&currpage=1&channelid=507980d214c943ebb0a70853ec94b12e';
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/xwsp/nav_list_js.shtml';
|
||||
title = '苏州市政府 - 新闻视频';
|
||||
break;
|
||||
case 'rdzt':
|
||||
|
|
@ -49,56 +54,84 @@ module.exports = async (ctx) => {
|
|||
title = '苏州市政府 - 区县专题';
|
||||
break;
|
||||
case 'zwgg':
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/zwgg/nav_list.shtml';
|
||||
urljs = 'http://www.suzhou.gov.cn/szinf/info/getInfoCommon/?pagesize=15&currpage=1&channelid=260915178a1f4c4fac44c4bf6378c9b0';
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/zwgg/nav_list_js.shtml';
|
||||
title = '苏州市政府 - 政务公告';
|
||||
break;
|
||||
case 'bmzx':
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/bmzx/nav_list.shtml';
|
||||
urljs = 'http://www.suzhou.gov.cn/szinf/info/getInfoCommon/?pagesize=15&currpage=1&channelid=b015bfa5e5514cc9a26cd9f956ef8e69';
|
||||
url = 'http://www.suzhou.gov.cn/szsrmzf/bmdt/nav_list_js.shtml';
|
||||
title = '苏州市政府 - 便民资讯';
|
||||
break;
|
||||
default:
|
||||
logger.error('pattern not matched');
|
||||
}
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: url,
|
||||
Host: 'www.suzhou.gov.cn',
|
||||
responseType: 'buffer',
|
||||
});
|
||||
|
||||
const $ = cheerio.load(response.data);
|
||||
const list = $('div.cont > ul > li');
|
||||
|
||||
ctx.state.data = {
|
||||
title: title,
|
||||
link: url,
|
||||
item: await Promise.all(
|
||||
list
|
||||
.slice(1, 10)
|
||||
.map(async (_, item) => {
|
||||
item = $(item);
|
||||
if (urljs) {
|
||||
const responsejs = await got({
|
||||
method: 'get',
|
||||
url: urljs,
|
||||
Host: 'www.suzhou.gov.cn',
|
||||
});
|
||||
const jsdata = responsejs.data.infolist;
|
||||
|
||||
ctx.state.data = {
|
||||
title: title,
|
||||
link: url,
|
||||
item: await Promise.all(
|
||||
jsdata.slice(0, 10).map(async (item) => {
|
||||
// 获取全文
|
||||
const a = item.find('a');
|
||||
const link = liburl.resolve(root_url, a.attr('href'));
|
||||
const link = item.link.indexOf('http') === -1 ? liburl.resolve(root_url, item.link) : item.link;
|
||||
const description = await ctx.cache.tryGet(link, async () => {
|
||||
const fullText = await got({
|
||||
method: 'get',
|
||||
url: link,
|
||||
Host: 'www.suzhou.gov.cn',
|
||||
responseType: 'buffer',
|
||||
});
|
||||
const fullTextData = cheerio.load(fullText.data);
|
||||
return fullTextData('div.main div.contentShow').html();
|
||||
let responsehtml;
|
||||
|
||||
try {
|
||||
responsehtml = await got({
|
||||
method: 'get',
|
||||
url: link,
|
||||
});
|
||||
const content = util.content(responsehtml.data);
|
||||
return content;
|
||||
} catch (error) {
|
||||
return '';
|
||||
}
|
||||
});
|
||||
return {
|
||||
title: a.text(),
|
||||
title: item.title,
|
||||
description: description,
|
||||
link: link,
|
||||
pubDate: new Date(item.find('span').text().trim() + ' GMT+8').toUTCString(),
|
||||
pubDate: item.pubtime,
|
||||
};
|
||||
})
|
||||
.get()
|
||||
),
|
||||
};
|
||||
),
|
||||
};
|
||||
}
|
||||
if (!urljs) {
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: url,
|
||||
});
|
||||
|
||||
const $ = cheerio.load(response.data);
|
||||
const list = $('div.pageList li');
|
||||
|
||||
ctx.state.data = {
|
||||
title: title,
|
||||
link: url,
|
||||
item: await Promise.all(
|
||||
list
|
||||
.slice(0, 10)
|
||||
.map(async (_, item) => {
|
||||
item = $(item);
|
||||
const a = item.find('a');
|
||||
const link = liburl.resolve(root_url, a.attr('href'));
|
||||
return {
|
||||
title: a.attr('title'),
|
||||
link: link,
|
||||
pubDate: new Date(item.find('.time').text()).toUTCString(),
|
||||
};
|
||||
})
|
||||
.get()
|
||||
),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
const cheerio = require('cheerio');
|
||||
|
||||
const content = (response) => {
|
||||
const e = cheerio.load(response);
|
||||
e('.article-extended').remove();
|
||||
e('#shareNode').remove();
|
||||
e('.article-auxiliary').remove();
|
||||
const content = e('.contentShow').html()
|
||||
? e('.contentShow').html()
|
||||
: e('div.detail')
|
||||
.html()
|
||||
.replace(/<.?ucaptitle>/g, '')
|
||||
.replace(/<.?ucapcontent>/g, '')
|
||||
.replace(/"\/szsrmzf/g, '"http://www.suzhou.gov.cn/szsrmzf')
|
||||
.replace('publishtime', 'span');
|
||||
return content;
|
||||
};
|
||||
module.exports = {
|
||||
content,
|
||||
};
|
||||
Loading…
Reference in New Issue