|
@@ -0,0 +1,135 @@
|
|
|
+/* eslint-disable strict */
|
|
|
+// const svgCaptcha = require('svg-captcha');
|
|
|
+// const cheerio = require('cheerio');
|
|
|
+// const puppeteer = require('puppeteer');
|
|
|
+const charset = require('superagent-charset');
|
|
|
+const superagent = charset(require('superagent'));
|
|
|
+const cheerio = require('cheerio');
|
|
|
+const fs = require('fs');
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
+class CreeperjlsyService extends CrudService {
|
|
|
+
|
|
|
+ async creeper() {
|
|
|
+ // 目标链接 吉林省教师招聘网第一页
|
|
|
+ const targetUrl = 'http://www.zgsydw.com/jilin/jiaoshi/index.html';
|
|
|
+ const columnTitle = '吉林省教师招聘';
|
|
|
+ // 查看是否有这个栏目,没有则创建一个
|
|
|
+ let column = await this.service.column.model.find({ news_type: '0', title: columnTitle });
|
|
|
+ if (column.length === 0) {
|
|
|
+ column = await this.service.column.model.create({ site: '99991', news_type: '0', title: columnTitle, type: '', parent_id: '', parent: '', is_use: '' });
|
|
|
+ } else {
|
|
|
+ column = column[0];
|
|
|
+ }
|
|
|
+ await this.creeperCreate(targetUrl, column);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 输入路径返回文本
|
|
|
+ async creeperCreate(targetUrl, column) {
|
|
|
+ // 目标链接 吉林省人事考试网第一页
|
|
|
+ // 用来暂时保存解析到的内容和图片地址数据
|
|
|
+ const hrefOld = targetUrl;
|
|
|
+ const hrefAdd = 'http://www.zgsydw.com';
|
|
|
+ const uri = '';
|
|
|
+ // const imgs = [];
|
|
|
+ // 创建附件文件夹(暂定直接跳到该网站下载)
|
|
|
+ // this.mkdir('./attachment');
|
|
|
+
|
|
|
+ // 发起请求
|
|
|
+ superagent.get(targetUrl).charset('gbk').buffer(true)
|
|
|
+ .end((error, res) => {
|
|
|
+ if (error) { // 请求出错,打印错误,返回
|
|
|
+ console.log(error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // cheerio需要先load html
|
|
|
+ const $ = cheerio.load(res.text);
|
|
|
+ // 循环列表,获取标题、a标签路径、日期,然后根据a标签路径再次进行爬出内容、保存即可
|
|
|
+ $('.ggxx_nr ul li').each((index, element) => {
|
|
|
+ // 这些数据都是存放在news中的
|
|
|
+ const title = $(element).find('a').attr('title');
|
|
|
+ const thisHref = $(element).find('a').attr('href');
|
|
|
+ const time = $(element).find('span').text();
|
|
|
+ // 这里可以给时间做判断当前日期(如果需要的话可以做为判断条件)
|
|
|
+ const nowDate = new Date().toLocaleDateString();
|
|
|
+ if (time !== nowDate) {
|
|
|
+ // 为undefined时,不需要进行下一步了
|
|
|
+ if (thisHref !== undefined) {
|
|
|
+ // 请求内容
|
|
|
+ superagent.get(thisHref).charset('gbk').buffer(true)
|
|
|
+ .end((error, res) => {
|
|
|
+ if (error) { // 请求出错,打印错误,返回
|
|
|
+ console.log(error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const $ = cheerio.load(res.text);
|
|
|
+ // 获取内容保存
|
|
|
+ // const content = $(element).children('table').clone();
|
|
|
+ const content = $('.con_l_list').text();
|
|
|
+ // content.find(':nth-child(n)').remove();
|
|
|
+ // console.log(content.text());
|
|
|
+ // 内容中存在翻页可能
|
|
|
+ const href = $('.lb_page a').attr('href');
|
|
|
+ if (href !== undefined) {
|
|
|
+ this.nextPage(href);
|
|
|
+ }
|
|
|
+
|
|
|
+ const attachment = [];
|
|
|
+ // 查看内容是否有翻页
|
|
|
+
|
|
|
+
|
|
|
+ // 每页都查询是否有附件存在,如果有,下载到本地,保存即可
|
|
|
+ $('p a').each((index, element) => {
|
|
|
+ const thisHref = $(element).attr('href');
|
|
|
+ if (thisHref.substring(0, 4) !== 'http') {
|
|
|
+ const url = uri + thisHref;
|
|
|
+ const fileName = $(element).text();
|
|
|
+ // console.log(fileName);
|
|
|
+ // console.log(uri);
|
|
|
+ // const filepath = this.downloadAttachment(url, fileName);
|
|
|
+ const file = {
|
|
|
+ name: fileName,
|
|
|
+ uri: url,
|
|
|
+ };
|
|
|
+ attachment.push(file);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // const news = this.service.news.model.create({
|
|
|
+ // site: column.site,
|
|
|
+ // title,
|
|
|
+ // pic: '',
|
|
|
+ // content,
|
|
|
+ // type: '',
|
|
|
+ // parent_id: column.id,
|
|
|
+ // parent: column.title,
|
|
|
+ // publish_time: publishTime,
|
|
|
+ // attachment,
|
|
|
+ // is_use: '0'
|
|
|
+ // });
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 点击下一页
|
|
|
+ const href = $('#DivPageControl a').eq(2).attr('href');
|
|
|
+ const hrefNew = hrefAdd + href;
|
|
|
+ // 第一次路径与第二次路径比较,相同,就不需要调自己了
|
|
|
+ if (hrefNew !== hrefOld) {
|
|
|
+ // this.creeperCreate(hrefNew, column);
|
|
|
+ // over
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ async nextPage(href) {
|
|
|
+ superagent.get(href).charset('gbk').buffer(true)
|
|
|
+ .end((error, res) => {
|
|
|
+ if (error) { // 请求出错,打印错误,返回
|
|
|
+ console.log(error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const $ = cheerio.load(res.text);
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|
|
|
+module.exports = CreeperjlsyService;
|