💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
~~~ 'use strict'; /** * cheerio 模块的使用 * 1.安装 npm i cheerio --save * 2.引入cheerio模块 const cheerio = require('cheerio'); * 3.加载要解析的内容 const $=cheerio.load('<h2 class="title">Hello World</h2>') * 4.用法 * $('.title').html() 获取了要匹配的标题的内容 * 5.解决汉字是乱码问题 * const $ = cheerio.load(htmlData, { decodeEntities: false }); * */ const cheerio = require('cheerio'); module.exports = { schedule: { interval: '5s', type: 'all', }, async task(ctx) { const url = 'https://news.baidu.com'; const result = await ctx.service.spider.requestUrl(url); const htmlData = result.data.toString(); // 检测网站是琐被篡改 网站是否宕机 const $ = cheerio.load(htmlData, { decodeEntities: false }); const title = $('title') .html(); console.log(title); if (title !== '百度新闻——海量中文资讯平台') { console.log('网站挂掉了,或者被修改了!'); } else { console.log('正常...'); } $('.hotnews a') .each(function() { console.log($(this) .html()); }); }, }; ~~~