狗东的LAMY钢笔昨天又在搞活动,结果看到的时候已经涨回原价了。一气之下,写了一个JS脚本,自动查询促销信息,并通过微信通知我。数据采集??准备在数据采集方面,我们使用Node.js实现了一个爬虫,定时访问刷新页面,随时监控数据。使用的依赖如下:cheerio:JS网页解析,语法类似JQuerysuperagent:网络请求库system-sleep:调用系统sleepmoment:格式化时间,方便记录消息通知微信通过Serversauce通知我们,什么是这?羊毛布?什么是ServerSauce“ServerSauce”,英文名称“ServerChan”,是一款“程序员”与“服务器”之间的交流软件。说英语?是一款将告警和日志从服务器端推送到手机端的工具。激活使用只需一分钟:登录:使用GitHub账号登录网站,您将获得一个SCKEY(在“发送消息”页面)绑定:点击“微信推送”,扫码领取同时关注完成绑定发送消息:发送GET请求到http://sc.ftqq.com/SCKEY.send,微信会收到消息。Serversauce的使用方法就这么简单。注册绑定后,你会得到一个网址,我们只需要给这个网址发消息就可以接收微信通知了。代码实现varcheerio=require('cheerio');varsuperagent=require('superagent')varsleep=require('system-sleep');varmoment=require('moment')//URLsvarbase='http://http://www.baicaio.com/'varcollection='http://www.baicaio.com/index-index-type--tab-isnice-dss-cc'varwechat='http://sc.ftqq.com/##Yourownurl.send'//哦摩西罗伊那个是要注意的关键字varomxly=['灵妹','耳机','笔','鼠标','Display','Computer','MobilePhone','TV']//通知内容的href,防止同一内容多次通知varnotified=[]functionrefresh(callback){console.log('查看促销信息'+moment().format('MMMMDoYYYY,h:mm:ssa'))varitems=[]superagent.get(collection).end(function(err,res){var$=cheerio.load(res.text)$('#C_drc').find('li').each(function(idx,element){$element=$(element)varhref=$element.find('h2>a').attr('href')vartitle=$element.find('h2>a').attr('title')varprice=$element.find('h2&g吨;a>em').text()items.push({href:href,title:title,price:price})})check(items,callback)})}functioncheck(items,callback){for(索引在items){varitem=items[index]for(word_indexinomxly){varflag=truevarword=omxly[word_index]if(String(item.title).indexOf(omxly[word_index])!==-1){for(href_indexinnotificated){if(notificated[href_index]==item.href){flag=falsecallback()break}}if(flag){console.log('发现关注商品')console.log(item.title,item.price)notificated.push(item.href)wechatNotification(word,item.title,item.price,item.href,callback)}}}}}functionwechatNotification(word,title,desc,url,callback){vartext='主人,我找到了你喜欢的东西:_'+''+worddesc='['+title+']('+base+url+')\n\n'+descdesc+='\n\n'desc+='time:'+moment().format('MMMMDoYYYY,h:mm:ssa')superagent.get(wechat).query({text:text}).query({desp:desc}).end(function(err,sres){callback()})}while(true){刷新({})睡眠(5*1000)}
