1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- /* eslint-disable strict */
- const Service = require('egg').Service;
- const svgCaptcha = require('svg-captcha');
- // const cheerio = require('cheerio');
- // const puppeteer = require('puppeteer');
- const request = require('superagent');
- const cheerio = require('cheerio');
- const fs = require('fs');
- class ToolsService extends Service {
- // 产生验证码
- async captcha() {
- const captcha = svgCaptcha.create({
- size: 4, // 大小
- fontSize: 50, // 字体大小
- width: 100, // 宽
- height: 40, // 高
- bacground: '#cc9966', // 背景色
- });
- return captcha;
- }
- // 发送短信验证码
- async sendmessage(ctx, app, randomstr) {
- const message = '【吉林省就业中心】您的验证码为:' + randomstr + ',请在30分钟内完成输入,欢迎使用吉林省智慧就业企业服务平台。';
- const data = '?Id=300&Name=wwqcgh&Psw=jljyzx-wwqcgh&Message=' + message + '&Phone=' + ctx.query.mobile + '&Timestamp=0';
- // const data = '?Id=300&Name=wwqcgh&Psw=jljyzx-wwqcgh&Message=' + message + '&Phone=' + phone + '&Timestamp=0';
- const path = ctx.app.config.messageDir + data;
- const result = await app.curl(path, {
- method: 'GET',
- dataType: 'text/xml',
- });
- return result;
- }
- // 输入路径返回文本
- async creeper() {
- // 目标链接 吉林省人事考试网第一页
- const targetUrl = 'http://www.jlzkb.com/cms/root/ksbmList.vm?dir=L-iAg-ivleaKpeWQjS_kuovkuJrljZXkvY3mi5vogZjogIPor5U&page=1&rows=8';
- // 用来暂时保存解析到的内容和图片地址数据
- const content = '';
- // const imgs = [];
- // 发起请求
- request.get(targetUrl).end((error, res) => {
- if (error) { // 请求出错,打印错误,返回
- console.log(error);
- return;
- }
- // cheerio需要先load html
- const $ = cheerio.load(res.text);
- // 循环列表,获取标题、a标签路径、日期,然后根据a标签路径再次进行爬出内容、保存即可
- $('#DivInfoList tr').each((index, element) => {
- // const title = $(element).find('td a').attr('title');
- // console.log(title);
- // const uri = $(element).find('td a').attr('href');
- // console.log(uri);
- // const time = $(element).find('td[width="12%"]').text();
- // console.log(time);
- });
- // 点击下一页
- console.log($('#DivPageControl').find('a').attr('href'));
- });
- }
- }
- module.exports = ToolsService;
|