由于有需求,因此就写了一个简单的脚原本实时监测房源。html
平时对node.js用的比较多,因此就用node.js来写,怎么方便咱怎么搞。node
直接提供打包好的代码,下载下来简单配置一些就能够运行了。前提是你的电脑要先安装好node.jsios
下载地址 :连接: https://pan.baidu.com/s/1boEHuQN 密码: vhicgit
下载完成后,修改app.js里的邮箱地址。代码里都注释都写好了,修改完保存,在house目录里双击 start.bat文件。github
一.房源信息抓取npm
经过对公寓申请网站的数据分析,找到了房源列表的接口地址。(吐槽一下,这个网站作的太烂了😆)axios
http://117.71.57.99:9080/online/roomResource.xp?action=formList1&code=01&buildingCode=0011449816806945psc http://117.71.57.99:9080/online/roomResource.xp?action=formList1&code=01&buildingCode=0011449816830250MuI http://117.71.57.99:9080/online/roomResource.xp?action=formList1&code=01&buildingCode=0011449816949458BXk http://117.71.57.99:9080/online/roomResource.xp?action=formList1&code=01&buildingCode=0011449816949458BXk
找到房源列表接口,下面就好办了。app
二.数据获取测试
1.首先咱们先要在电脑上安装node,node安装我就不写了,网上一大堆教程。(http://jingyan.baidu.com/article/fd8044faf2e8af5030137a64.html)网站
2.在你电脑桌面上鼠标右击,新建文件夹,进入文件夹 而后用命令行 输入
npm init
而后一路enter
2.按照必要的模块
axios(接口请求)
npm install axios -save
npm install nodemailer --save
3.开始写代码
const nodemailer = require('nodemailer');
const axios = require('axios') let i = 1; setInterval(()=> { console.log(`可入住房源第${i}次查询中...`) axios.get('http://117.71.57.99:9080/online/roomResource.xp?action=formList1&code=01&buildingCode=0011449816806945psc') .then(res=> { formatData(res.data.list, '1栋') }); axios.get('http://117.71.57.99:9080/online/roomResource.xp?action=formList1&code=01&buildingCode=0011449816830250MuI') .then(res=> { formatData(res.data.list, '2栋') }); axios.get('http://117.71.57.99:9080/online/roomResource.xp?action=formList1&code=01&buildingCode=0011449816876736sfx') .then(res=> { formatData(res.data.list, '综合楼东') }); axios.get('http://117.71.57.99:9080/online/roomResource.xp?action=formList1&code=01&buildingCode=0011449816949458BXk') .then(res=> { formatData(res.data.list, '综合楼西') }) i++ }, 10000) function formatData(list, info) { for (var key in list) { for (var j = 0; j < list[key].length; j++) { const roomInfo = list[key][j] let {id,status,roomFloor,roomName,roomType} = roomInfo if (status == 02 || status == 01) { axios.get(`http://117.71.57.99:9080/online/roomConfig.xp?action=getRoomConfig&roomID=${id}`).then(res => { let {itemName,roomTypeName,price,roomArea} = res.data.info; let roomDirection = res.data.roomDirection; sendEmail(info, roomFloor, roomName,roomDirection,roomTypeName,price,roomArea,itemName) }) } } } } function sendEmail(info, roomFloor, roomName,roomDirection,roomTypeName,price,roomArea,itemName) { // 开启一个 SMTP 链接池 let transporter = nodemailer.createTransport({ host: 'smtp.163.com', secureConnection: true, // use SSL port: 465, secure: true, // secure:true for port 465, secure:false for port 587 auth: { user: '', // 你的邮箱帐号 pass: '' // QQ邮箱须要使用受权码 //邮箱密码 } }); // 设置邮件内容(谁发送什么给谁) let mailOptions = { from: '"xxx" <marven@163.com>', // 发件人 to: 'xxx@qq.com', // 收件人 subject: `Hello ✔有可入住的房源啦`, // 主题 text: 'search house', // plain text body html: `<b style="font-size:18px;">已为你搜到可入住的房源啦</b> <br> <p style="font-size:22px">房间信息:${info}--${roomFloor}楼--${roomName}</p> <p style="font-size:22px;color'#db384c'">房间类型:${roomTypeName}</p> <p style="font-size:22px">房间价格:${price}元/月</p> <p style="font-size:22px">房间大小:${roomArea}m²米</p> <p style="font-size:22px">房间朝向:${roomDirection}</p> <p style="font-size:22px">房间配置:${itemName}</p> <a style="font-size:18px;color:blue" href="http://117.71.57.99:9080/online/gzflogin.jtml?action=login&accountCode=xxx&accountPass=xxx">当即登陆</a>`, }; // 使用先前建立的传输器的 sendMail 方法传递消息对象 transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log(`Message: ${info.messageId}`); console.log(`sent: ${info.response}`); }); }
而后保存为 app.js
实践的时候遇到许多问题,如今列举以下,若未详尽,敬请留言交流。
首先须要开启邮箱的 POP3/SMTP 服务。
QQ邮箱须要使用受权码,而不是QQ密码;163 邮箱直接使用163邮箱密码就行。
进入QQ邮箱,设置-帐户-开启服务 POP3/SMTP 服务,并生成受权码,如今获取受权码须要验证手机短信。
理论上支持全部主流邮箱,但我只测试了 QQ 和 163,都成功了。若其余邮箱出问题请留言交流。
Error: Invalid login: 535 Error: authentication failed
认证失败:
secureConnection: true,
smtp.qq.com
;163 的 host 是 smtp.163.com
Error: Mail command failed: 553 Mail from must equal authorized user
发件人和认证的邮箱地址不一致
在命令行 输入
node app.js
这样就能够自动刷房源了。
测试了一下,效果很好
代码已上传到github,欢迎各位给个star https://github.com/zhouyangit/searchHouse