node爬取墨迹天气数据发送定时邮件
生活随笔
收集整理的這篇文章主要介紹了
node爬取墨迹天气数据发送定时邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??使用vscode通過node.js爬取墨跡天氣網站的數據,然后通過郵件每天定時給特定的一個或者多個郵箱發送具有特定html樣式的163郵件。
實現爬取功能準備工作:
官網下載node.js
打開vs code建好項目,在終端輸入npm init -y初始化項目;在vs code終端輸入npm install superagent cheerio art-template node-schedule nodemailer安裝需要的各種依賴包。
還需要一個163郵箱以及郵箱客戶端授權密碼。然后就OK可以開始擼代碼啦~
main.js代碼(爬蟲以及計算時間部分代碼)
//引入superagent包,用于服務器發送http請求; const request = require('superagent'); //導入cheerio包,把字符串解析成html。 const cheerio = require('cheerio'); //導入模板引擎, const template = require('art-template'); //導入path模塊處理路徑 const path = require('path'); //導入Nodemailer包 const nodemailer = require("nodemailer"); //導入定時模塊包 var schedule = require('node-schedule');//計算放暑假的天數 function getDayData() {return new Promise((resolve, reject) => {//現在的時間const today = new Date();//暑假開始的時間const meet = new Date('2019-7-20');//計算暑假到今天的天數const count = Math.ceil((today - meet) / 1000 / 60 / 60 / 24);//今天日期格式化const format = today.getFullYear() + " / " + (today.getMonth() + 1) + " / " + today.getDate();const dayData = {count,format}// console.log(dayData);resolve(dayData);})} // getDayData(); //請求墨跡天氣的數據 function getMojiData() {return new Promise((resolve, reject) => {request.get('https://tianqi.moji.com/weather/china/henan/xinyang').end((err, res) => {if (err) return console.log("數據請求失敗~");// console.log(res.text);const $ = cheerio.load(res.text);//圖標const icon = $('.wea_weather span img').attr('src');//天氣const weather = $('.wea_weather b ').text();//溫度const temperature = $('.wea_weather em').text();console.log(temperature);//提示const tips = $('.wea_tips em').text();const MojiData = {icon,weather,temperature,tips}// console.log(mojiData);resolve(MojiData);})}) } // getMojiData(); //請求one頁面抓取數據 function getOneData() {return new Promise((resolve, reject) => {request.get('http://wufazhuce.com/').end((err, res) => {if (err) return console.log("數據請求失敗~");const $ = cheerio.load(res.text);// 爬取圖片const img = $('.carousel-inner>.item>a>img').eq(2).attr('src');//爬取文字const text = $('.fp-one .fp-one-cita-wrapper .fp-one-cita a').eq(2).text();const OneData = {img,text}// console.log(OneData);resolve(OneData);})}) } // getOneData();// 通過模板引擎替換html的數據 async function renderTemplate() {//獲取 日期const dayData = await getDayData();//獲取 墨跡天氣數據const MojiData = await getMojiData();//獲取one網頁數據const OneData = await getOneData();// console.log(dayData);// console.log(MojiData);// console.log(OneData);//當所有數據都獲取成功的時候,進行模板引擎數據的替換return new Promise((resolve, reject) => {const html = template(path.join(__dirname, "./email.html"), {dayData,MojiData,OneData});resolve(html);})} // renderTemplate(); async function sendNodeMail() {const html = await renderTemplate();// const html = "<h1>哈哈哈啊</h1>";console.log(html);// create reusable transporter object using the default SMTP transportlet transporter = nodemailer.createTransport({host: "smtp.163.com",port: 465,secure: true, // true for 465, false for other portsauth: {user: "XXXXX@163.com(你的郵箱)", // generated ethereal user用戶名pass: "XXXXX(你的郵箱客戶端授權密碼)" // generated ethereal password}});// send mail with defined transport objectlet mailOptions = {from: '"緩月" <XXXXX@163.com", // list of receivers收件人列表subject: "測試郵件~", // Subject linehtml: html // html body};transporter.sendMail(mailOptions, (error, info = {}) => {if (error) {console.log(error);sendNodeMail(); //再次發送}console.log("郵件發送成功", info.messageId);console.log("靜等下一次發送~");}) } var j = schedule.scheduleJob("00 26 15 * *", function() {sendNodeMail();console.log("發送郵件成功~"); })郵件模板樣式代碼
這個是你寫的郵件的樣式,可以理解為一個模板,即你每天發送郵件的固定樣式(可以根據自己的喜好進行更改~).
<!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>郵件</title> </head><body style="margin: 0;padding:0;"><div style="width:100%;margin:0 auto;padding: 20px;text-align: center;"><span style="font-size: 26px;font-weight:600;">今天是放暑假的第</span><span style="color: chocolate;font-size:36px;font-weight:bold;">{{ dayData.count }}</span><span style="font-size: 26px;font-weight:600;">天</span></div><p style="text-align:center;">小可耐,今天也要加油喔~</p><div style="width: 100%;margin:0 auto;color:lightsteelblue;text-align:center;"><img src="{{ MojiData.icon }}" alt="天氣icon" style="" /><b style="display:block;color:#333;font-size:24px;margin:15px 0;">天氣:{{ MojiData.weather }}</b><span style="display:block;color:#333;font-size:22px;margin:15px 0;">溫度:{{ MojiData.temperature }}</span><span>Tips:{{MojiData.tips}}</span></div><div style="text-align:center;margin: 35px 0;"><span style="display:block;margin-top:55px;color:#676767;font-size:15px;">ONE·一個</span><span style="display:block;margin-top:25px;color:#9d9d9d;font-size:22px;">{{ dayData.format }}</span><img src="{{ OneData.img }}" style="width:100%;margin-top:10px;" alt="今日圖片" /><div style="margin: 10px auto;margin-top:10px;">{{ OneData.text }}</div></div> </body></html>總結
以上是生活随笔為你收集整理的node爬取墨迹天气数据发送定时邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工学结合2019/9/17
- 下一篇: Vegas怎么制作古装墨迹笔刷开场效果