安裝依賴html
cnpm install cheerio --save
service/spider.js
'use strict'; const Service = require('egg').Service; class SpiderService extends Service { async requestUrl(url) { var result = await this.ctx.curl(url); return result; } } module.exports = SpiderService;
schedule/watchdomain.js
var cheerio = require('cheerio') module.exports = (app) => { return { schedule:{ interval:'10s', type:'all' }, async task(ctx){ var url = 'https://news.baidu.com/'; var result = await ctx.service.spider.requestUrl(url); //buff數據轉爲utf8 var htmlData = result.data.toString(); // 亂碼轉爲utf8 const $ = cheerio.load(htmlData,{decodeEntities:false}) // 拿到網站標題 var title = $('title').html(); if(title != '百度新聞——全球最大的中文新聞平臺'){ console.log("網站被修改了") }else{ console.log("正常") } //根據class拿到數據 $('.hotnews a').each(function(){ console.log($(this).html()) }) } } }