用node写一个皖水公寓自动刷房源脚本

因为有需求,所以就写了一个简单的脚本来实时监测房源。

平时对node.js用的比较多,所以就用node.js来写,怎么方便咱怎么搞。


 

2017.12.19号更新

热心的小伙伴告诉我,房源查询现在加了一层验证码,所以代码暂时不能使用了。请大家先手动查询吧,有时间我会看一下验证码的事情。

大家新年快乐!

 


 

直接提供打包好的代码,下载下来简单配置一些就可以运行了。前提是你的电脑要先安装好node.js

下载地址 :链接: https://pan.baidu.com/s/1boEHuQN   密码: vhic

下载完成后,修改app.js里的邮箱地址。代码里都注释都写好了,修改完保存,在house目录里双击 start.bat文件。

 

 

一.房源信息抓取

通过对公寓申请网站的数据分析,找到了房源列表的接口地址。(吐槽一下,这个网站做的太烂了😆)

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

找到房源列表接口,下面就好办了。

二.数据获取

1.首先我们先要在电脑上安装node,node安装我就不写了,网上一大堆教程。(http://jingyan.baidu.com/article/fd8044faf2e8af5030137a64.html

2.在你电脑桌面上鼠标右击,新建文件夹,进入文件夹 然后用命令行 输入 

npm init

然后一路enter

2.按照必要的模块

axios(接口请求)

nodemailer(用来发送邮件到自己邮箱)
 
在命令行输入
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为例)

首先需要开启邮箱的 POP3/SMTP 服务。

QQ邮箱需要使用授权码,而不是QQ密码;163 邮箱直接使用163邮箱密码就行。

进入QQ邮箱,设置-账户-开启服务 POP3/SMTP 服务,并生成授权码,现在获取授权码需要验证手机短信。

 
 

支持邮箱

理论上支持所有主流邮箱,但我只测试了 QQ 和 163,都成功了。若其他邮箱出问题请留言交流。

535 错误

Error: Invalid login: 535 Error: authentication failed

认证失败:

  • 可能是账号密码错误
  • 链接资源池时加 ssl:secureConnection: true,
  • QQ 的 host 是 smtp.qq.com;163 的 host 是 smtp.163.com

553 错误

Error: Mail command failed: 553 Mail from must equal authorized user

发件人和认证的邮箱地址不一致

  • auth.user 需要与 from 中的邮箱一致

在命令行 输入  

node app.js

 

这样就可以自动刷房源了。

测试了一下,效果很好

代码已上传到github,欢迎各位给个star      https://github.com/zhouyangit/searchHouse

posted @ 2017-08-21 06:19  _marven  阅读(5828)  评论(4)    收藏  举报