creeperjljs.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* eslint-disable strict */
  2. // const svgCaptcha = require('svg-captcha');
  3. // const cheerio = require('cheerio');
  4. // const puppeteer = require('puppeteer');
  5. const charset = require('superagent-charset');
  6. const superagent = charset(require('superagent'));
  7. const cheerio = require('cheerio');
  8. const fs = require('fs');
  9. const { CrudService } = require('naf-framework-mongoose/lib/service');
  10. class CreeperjlsyService extends CrudService {
  11. async creeper() {
  12. // 目标链接 吉林省教师招聘网第一页
  13. const targetUrl = 'http://www.zgsydw.com/jilin/jiaoshi/index.html';
  14. const columnTitle = '吉林省教师招聘';
  15. // 查看是否有这个栏目,没有则创建一个
  16. let column = await this.service.column.model.find({ news_type: '0', title: columnTitle });
  17. if (column.length === 0) {
  18. column = await this.service.column.model.create({ site: '99991', news_type: '0', title: columnTitle, type: '', parent_id: '', parent: '', is_use: '' });
  19. } else {
  20. column = column[0];
  21. }
  22. await this.creeperCreate(targetUrl, column);
  23. }
  24. // 输入路径返回文本
  25. async creeperCreate(targetUrl, column) {
  26. // 目标链接 吉林省人事考试网第一页
  27. // 用来暂时保存解析到的内容和图片地址数据
  28. const hrefOld = targetUrl;
  29. const hrefAdd = 'http://www.zgsydw.com';
  30. const uri = '';
  31. // const imgs = [];
  32. // 创建附件文件夹(暂定直接跳到该网站下载)
  33. // this.mkdir('./attachment');
  34. // 发起请求
  35. superagent.get(targetUrl).charset('gbk').buffer(true)
  36. .end((error, res) => {
  37. if (error) { // 请求出错,打印错误,返回
  38. console.log(error);
  39. return;
  40. }
  41. // cheerio需要先load html
  42. const $ = cheerio.load(res.text);
  43. // 循环列表,获取标题、a标签路径、日期,然后根据a标签路径再次进行爬出内容、保存即可
  44. $('.ggxx_nr ul li').each((index, element) => {
  45. // 这些数据都是存放在news中的
  46. const title = $(element).find('a').attr('title');
  47. const thisHref = $(element).find('a').attr('href');
  48. const time = $(element).find('span').text();
  49. // 这里可以给时间做判断当前日期(如果需要的话可以做为判断条件)
  50. const nowDate = new Date().toLocaleDateString();
  51. if (time !== nowDate) {
  52. // 为undefined时,不需要进行下一步了
  53. if (thisHref !== undefined) {
  54. // 请求内容
  55. superagent.get(thisHref).charset('gbk').buffer(true)
  56. .end((error, res) => {
  57. if (error) { // 请求出错,打印错误,返回
  58. console.log(error);
  59. return;
  60. }
  61. const $ = cheerio.load(res.text);
  62. // 获取内容保存
  63. // const content = $(element).children('table').clone();
  64. const content = $('.con_l_list').text();
  65. // content.find(':nth-child(n)').remove();
  66. // console.log(content.text());
  67. // 内容中存在翻页可能
  68. const href = $('.lb_page a').attr('href');
  69. if (href !== undefined) {
  70. this.nextPage(href);
  71. }
  72. const attachment = [];
  73. // 查看内容是否有翻页
  74. // 每页都查询是否有附件存在,如果有,下载到本地,保存即可
  75. $('p a').each((index, element) => {
  76. const thisHref = $(element).attr('href');
  77. if (thisHref.substring(0, 4) !== 'http') {
  78. const url = uri + thisHref;
  79. const fileName = $(element).text();
  80. // console.log(fileName);
  81. // console.log(uri);
  82. // const filepath = this.downloadAttachment(url, fileName);
  83. const file = {
  84. name: fileName,
  85. uri: url,
  86. };
  87. attachment.push(file);
  88. }
  89. });
  90. // const news = this.service.news.model.create({
  91. // site: column.site,
  92. // title,
  93. // pic: '',
  94. // content,
  95. // type: '',
  96. // parent_id: column.id,
  97. // parent: column.title,
  98. // publish_time: publishTime,
  99. // attachment,
  100. // is_use: '0'
  101. // });
  102. });
  103. }
  104. }
  105. });
  106. // 点击下一页
  107. const href = $('#DivPageControl a').eq(2).attr('href');
  108. const hrefNew = hrefAdd + href;
  109. // 第一次路径与第二次路径比较,相同,就不需要调自己了
  110. if (hrefNew !== hrefOld) {
  111. // this.creeperCreate(hrefNew, column);
  112. // over
  113. }
  114. });
  115. }
  116. async nextPage(href) {
  117. superagent.get(href).charset('gbk').buffer(true)
  118. .end((error, res) => {
  119. if (error) { // 请求出错,打印错误,返回
  120. console.log(error);
  121. return;
  122. }
  123. const $ = cheerio.load(res.text);
  124. });
  125. }
  126. }
  127. module.exports = CreeperjlsyService;