<p>Node使用100行代码实现抓取某用户最新内容且发送提醒,主要使用了superagent ,
cheerio,nodemailer,node-schedule四个模块代码如下,修改相关配置信息即可运行。
</p>
let request = require('superagent');
let cheerio = require('cheerio');
let nodemailer = require('nodemailer');
let schedule = require('node-schedule');
//UID获取可在想抓取的新浪微博用户首页查看源代码获得
//修改uid
let reqUrl = 'http://service.weibo.com/widget/widget_blog.php?uid=你要抓取的用户UID数字';
let oldVal;
let newVal;
//邮件服务器配置信息
let transport = nodemailer.createTransport({
//相关邮件服务器配置可在https://nodemailer.com/about/ 查看
service: 'qq',//qq邮箱
auth: {
user: '发件邮箱账户',
pass: '密码'
}
});
//邮件发送配置选项
var options = {
from: '发件邮箱账户',
to: '目标账户',
subject: '邮件主题!',
text: '邮件内容'
};
function requestUrl(url) {
//获得微博信息
return request.get(url).then((res) => {
var $ = cheerio.load(res.text);
//内容
var msgcontent = [];
$('.wgtCell .wgtCell_con').each(function(index, dom) {
var msg = {};
msg.inner = $(this).find('.wgtCell_txt').text();
msg.timer = $(this).find('.wgtCell_tm').text();
msgcontent.push(msg);
});
console.log('最新微博:' + msgcontent[0].inner + ':' + msgcontent[0].timer);
return msgcontent
})
};
function tasks(url, ms, to) {
//url 完整请求地址
//ms 每分钟第几秒开始任务
//to 目标邮箱,接受邮件提醒的邮箱地址
//获取任务启动时初始微博
requestUrl(url).then(res => { oldVal = res[0].inner });
//定时任务获取新微博
schedule.scheduleJob({ second: ms }, () => {
console.log('任务运行于:' + new Date());
requestUrl(url).then(res => {
//获得最新微博并且于初始值比对
newVal = res[0].inner;
if (oldVal != newVal) {
//修改发送邮件配置项
options.to = to;
options.text = '你关注人发了微博:' + newVal + '于:' + res[0].timer;
transport.sendMail(options, function(error, info) {
if (error) {
console.log('发送失败!' + error)
} else {
console.log('发送成功!' + to);
//发送邮件成功则重置初始微博
requestUrl(url).then(res => { oldVal = res[0].inner });
}
})
} else {
console.log('微博未更新!')
}
});
})
}
tasks('完整请求地址', '第几秒启动', '目标邮箱')
网友评论