js提取用户新浪邮箱中的信用卡信息,是js非nodejs。
对比py,之前就做不好,出现了复杂点选验证码。js的开发速度只需要py的三分之一,甚至十分之一。
js在客户端执行,py在后端执行,py要实现实时高并发,在应对2000个并发时候,需要买几百台服务器。js节约这些钱。如果某一步使用了selenium需要上千台服务器。
可以登录后打开邮箱页面时候,执行这个来测试。就能获取到别人的信息了。实际是app。
一定是要在新浪邮箱页面打开console控制台,不能在别的网站打开console执行下面的代码,否则是跨域的,请求不到,要不然开发者在自己网站的js脚本写个ajax,请求淘宝 支付宝的网站api,然后把结果传到后台,你的信息都被别人知道了,这是不可能做到的。
//消除空格 String.prototype.trim = function() { return this.replace(/(^s*)|(s*$)/g, ""); }; //正则防止出错 String.prototype.match2 = function(regObj) { var matchArray = this.match(regObj); if (matchArray && matchArray.length == 2){ return matchArray[1]; } else{ return ''; } }; function getInnerText( cssselector){ var element = document.querySelector(cssselector); if(element){ return element.innerText; } else { console.error("没有找到 " + cssselector + " 的元素"); return ""; } } //获取元素 function myParseFunction(domObj, cssselector){ var obj = domObj.querySelector(cssselector); if(obj){ return obj.innerText; } else{ console.error("没有找到 " + cssselector + " 的元素"); return ""; } } // 定义全局变量 //var sid = window.location.href.match(/sid=(S+)#/)[1]; var KEYWORD_DICK = { "jiaotong":"交通银行信用卡电子账单", "jianshe": "中国建设银行信用卡电子账单", "zhaoshang":"招商信用卡月账单", "guangfa":"广发信用卡电子账单", "pingan":"平安信用卡月对账单", "zhongxin": "中信信用卡月账单" }; //存放每个银行的邮件id midObj = {}; for (var key in KEYWORD_DICK){ midObj[key] = []; } function searchMail(keyword){ console.debug('开始搜索' + KEYWORD_DICK[keyword] +'的所有邮件id'); var content = KEYWORD_DICK[keyword]; var url = 'https://mail.aliyun.com/alimail/ajax/mail/queryMailList.txt?_timestamp_=1517642178006'; var data = { showFrom:1, query:'{"keywordFields":5,"keywords":["' + content +'"],"advancedSearch":true}', fragment:-1, offset:0, length:25, _csrf_token_:$.cookie('_csrf_token_'), _root_token_:$.cookie('_root_token_'), _refer_hash_:window.location.href.match(/#(.*)/)[1], _tpl_:'DEFAULT' }; var res = $.ajax({ type: "POST", url:url, data: data, async: false }); var htmlStr = res.responseText; console.debug('搜索邮件的源码是:' + htmlStr); var midsMatch = htmlStr.match(/mailId":".*?"/g) || []; //console.debug(midsMatch); for (var index=0; index<midsMatch.length; index++){ var mid = midsMatch[index].match(/mailId":"(.*?)"/)[1]; midObj[keyword].push(mid); } //console.debug(midObj); } // searchMail('jianshe') function readMail(mid) { // url https://mail.163.com/js6/read/readhtml.jsp?mid=364:xtbBbBqIbVc6-6WyKQAAs-_30c7:bdc2:h:b:8&font=15&color=064977 var url = "https://mail.aliyun.com/alimail/ajax/mail/loadMail.txt?_timestamp_=1517645971062"; var data = { mailId:mid, charset:'', full:1, allowAutoQuote:1, _csrf_token_:$.cookie('_csrf_token_'), _root_token_:$.cookie('_root_token_'), _refer_hash_:h=window.location.href.match(/#(.*)/)[1], _tpl_:'DEFAULT' }; console.debug('请求邮件详情的url是: ' + url); var res = $.ajax({ type: "POST", url:url, data: data, async: false }); //console.debug(res.responseText); var content = res.responseText.match2(/({[sS]*})/);//.match2(/<string name="content">([sS]*?)</string>/); // console.debug('获取的邮件详情是:'); // console.debug(content); //下面是转义,例如<转为< var ifame = document.createElement("ifame"); ifame.innerHTML = content; var resText0 = ifame.innerText; //console.debug('resText',resText); var contentObj = JSON.parse(resText0); var resText1 = contentObj['data']['body']; var ifame = document.createElement("ifame"); ifame.innerHTML = resText1; var resText = ifame.innerHTML; console.debug('resTextstart ',resText,'resTextend '); return resText; } function parseZhaoShang(resText) { var ifame = document.createElement("ifame"); ifame.innerHTML = resText; var data = {}; data['username'] = resText.match2(/尊敬的s?(S+)s?[先|生|女|士]{2}/); data["bill_month"] = resText.match2(/以下是您的招商银行信用卡(d+月)账单/); data["bank"] = "招商银行"; if (RegExp("尊敬的").test(resText)){ //详细账单 data["due_date"] = myParseFunction(ifame, "#fixBand42 > table > tbody > tr > td > table > tbody > tr > td:nth-child(3) > div > font"); data["need_pay"] = myParseFunction(ifame, "#fixBand40 > table > tbody > tr > td > table > tbody > tr > td:nth-child(3) > div > font"); data["min_need_pay"]=myParseFunction(ifame, "#fixBand40 > table > tbody > tr > td > table > tbody > tr > td:nth-child(5) > div > font"); data["card_limit"] = myParseFunction(ifame, "#fixBand38 > table > tbody > tr > td > table > tbody > tr > td:nth-child(5) > div > font"); data["cash_limit"] = ""; data["bill_peroid"] = myParseFunction(ifame, "#fixBand38 > table > tbody > tr > td > table > tbody > tr > td:nth-child(3) > div > font"); data["avb_point"] = myParseFunction(ifame, "#fixBand23 > table > tbody > tr > td > table > tbody > tr > td:nth-child(2) > div > font"); data["card_number"] = ""; data["last_bill"] = myParseFunction(ifame, "#fixBand13 > table > tbody > tr > td > table > tbody > tr > td:nth-child(3) > div > font"); data["bill_new"] = myParseFunction(ifame, "#fixBand13 > table > tbody > tr > td > table > tbody > tr > td:nth-child(5) > div > font"); data["bill_paid"] = myParseFunction(ifame, "#fixBand13 > table > tbody > tr > td > table > tbody > tr > td:nth-child(4) > div > font"); // data["bill_left"] = document.querySelector().innerText; data["bill_adjust"] = myParseFunction(ifame, "#fixBand13 > table > tbody > tr > td > table > tbody > tr > td:nth-child(6) > div > font"); data["cycle_interest"] = myParseFunction(ifame, "#fixBand13 > table > tbody > tr > td > table > tbody > tr > td:nth-child(7) > div > font "); } else{//简约账单 data["due_dae"] = myParseFunction(ifame, "#fixBand18 > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td:nth-child(2) > div > font"); data["nend_pay"] = myParseFunction(ifame, "#fixBand57 > table > tbody > tr > td > table > tbody > tr > td:nth-child(2) > div > font"); data["min_need_pay"] = myParseFunction(ifame, "#fixBand46 > table > tbody > tr > td > table > tbody > tr > td:nth-child(2) > div > font"); } return data } function parseGuangFa(resText) { var ifame = document.createElement("ifame"); ifame.innerHTML = resText; var data = {}; data['username'] = resText.match2(/尊敬的s?(S+)s?[先|生|女|士]{2}/); data["bill_month"] = resText.match2(/(d+年d+月)/); data["bank"] = "广发银行"; if (RegExp("感谢您使用广发银行信用卡").test(resText)) { //2015年12月(含)以后的账单 data["bill_peroid"] = resText.match2(/账单周期:([sd/至]+)/); if (RegExp("积分明细").test(resText)) { //详细账单,2016年8月(含)以后邮件不再显示账单明细 data["due_date"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(5) > div > font"); data["need_pay"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(3) > div > font"); data["min_need_pay"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(4) > div > font"); data["card_limit"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(7) > div > font"); // data["cash_limit"] = ""; data["avb_point"] = myParseFunction(ifame, "#fixBand7 > table > tbody > tr > td > table > tbody > tr > td:nth-child(7) > div > font"); data["card_number"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(2) > div > font"); data["last_bill"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(5) > div > font"); data["bill_new"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(6) > div > font"); data["bill_paid"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(7) > div > font"); // data["bill_adjust"] = myParseFunction(ifame, ""); // data["cycle_interest"] = myParseFunction(ifame, ""); } else { //2016年8月以后的账单 data["due_date"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(5) > div > font"); data["need_pay"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(3) > div > font"); data["min_need_pay"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(4) > div > font"); data["card_limit"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(7) > div > font"); // data["cash_limit"] = ""; // data["avb_point"] = myParseFunction(ifame, ""); data["card_number"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(2) > div > font"); data["last_bill"] = myParseFunction(ifame, "#reportPanel2 #fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(5) > div > font"); data["bill_new"] = myParseFunction(ifame, "#reportPanel2 #fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(6) > div > font"); data["bill_paid"] = myParseFunction(ifame, "#reportPanel2 #fixBand4 > table > tbody > tr > td > table > tbody > tr > td:nth-child(7) > div > font"); // data["bill_adjust"] = myParseFunction(ifame, ""); // data["cycle_interest"] = myParseFunction(ifame, ""); }; } else{ //2015年11月以前的账单 data["bill_peroid"] = myParseFunction(ifame, "#fixBand5 > table > tbody > tr > td > table > tbody > tr > td:nth-child(2) > div > font"); data["due_date"] = myParseFunction(ifame, "#fixBand7 > table > tbody > tr > td:nth-child(4) > div > font"); data["need_pay"] = myParseFunction(ifame, "#fixBand7 > table > tbody > tr > td:nth-child(2) > div > font"); data["min_need_pay"]=myParseFunction(ifame, "#fixBand7 > table > tbody > tr > td:nth-child(3) > div > font"); data["card_limit"] = myParseFunction(ifame, "#fixBand5 > table > tbody > tr > td > table > tbody > tr > td:nth-child(4) > div > font"); data["avb_point"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td:nth-child(9) > div > font"); data["card_number"] = myParseFunction(ifame, "#fixBand7 > table > tbody > tr > td:nth-child(1) > div > font"); data["last_bill"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td:nth-child(3) > div > font"); data["bill_new"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td:nth-child(5) > div > font"); data["bill_paid"] = myParseFunction(ifame, "#fixBand4 > table > tbody > tr > td:nth-child(7) > div > font"); } return data } function parsePingAn(resText) { var ifame = document.createElement("ifame"); ifame.innerHTML = resText; var data = {}; data['username'] = resText.match2(/尊敬的s?(S+)s?[先|生|女|士]{2}/); data["bill_month"] = myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(8) > td > table > tbody > tr > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(2) > td").match2(/以下是您(.+)的信用卡对账单/); data["bank"] = "平安银行"; data["due_date"] = myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(9) > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(2) > td > table > tbody > tr:nth-child(2) > td:nth-child(2) > span > strong"); data["need_pay"] = myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(9) > td > table > tbody > tr > td:nth-child(3) > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td:nth-child(2) > table > tbody > tr:nth-child(1) > td "); data["min_need_pay"]=myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(9) > td > table > tbody > tr > td:nth-child(3) > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td:nth-child(2) > table > tbody > tr:nth-child(3) > td"); data["card_limit"] = myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(9) > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(2) > td > table > tbody > tr:nth-child(3) > td:nth-child(2)"); data["cash_limit"] = myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(9) > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(2) > td > table > tbody > tr:nth-child(4) > td:nth-child(2)"); // data["bill_peroid"] = myParseFunction(ifame, ""); data["avb_point"] = myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(13) > td > table > tbody > tr:nth-child(3) > td:nth-child(1)"); // data["card_number"] = ""; data["last_bill"] = myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(21) > td > table > tbody > tr:nth-child(2) > td:nth-child(2)"); data["bill_new"] = myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(21) > td > table > tbody > tr:nth-child(2) > td:nth-child(4)"); data["bill_paid"] = myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(21) > td > table > tbody > tr:nth-child(2) > td:nth-child(3)"); data["bill_adjust"] = myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(21) > td > table > tbody > tr:nth-child(2) > td:nth-child(5)"); data["cycle_interest"] = myParseFunction(ifame, "#mailContentContainer > table > tbody > tr:nth-child(21) > td > table > tbody > tr:nth-child(2) > td:nth-child(6)"); return data } function parseZhongXin(resText) { //var resText = document.documentElement.outerHTML; var ifame = document.createElement("ifame"); ifame.innerHTML = resText; var data = {}; data["bank"] = "中信银行"; data['username'] = resText.match2(/尊敬的s?(S+)s?[先|生|女|士]{2}/); data["bill_month"] = resText.match2(/(d+年d+月)账单已产生/); data["bill_peroid"] = resText.match2(/记录了您(S+)账户变动信息/); data["due_date"] = resText.match2(/到期还款日:([d年月日]+)/); data["need_pay"] = myParseFunction(ifame, "#fixBand16 > table > tbody > tr > td > table > tbody > tr > td:nth-child(3) > div > font"); data["min_need_pay"]=myParseFunction(ifame, "#fixBand11 > table > tbody > tr > td > table > tbody > tr > td:nth-child(3) > div > font"); data["card_limit"] = myParseFunction(ifame, "#fixBand12 > table > tbody > tr:nth-child(1) > td > table > tbody > tr > td:nth-child(3) > div > font"); data["cash_limit"] = myParseFunction(ifame, "#fixBand13 > table > tbody > tr:nth-child(1) > td > table > tbody > tr > td:nth-child(3) > div > font"); // data["avb_point"] = myParseFunction(ifame, ""); data["card_number"] = myParseFunction(ifame, "#fixBand5 > table > tbody > tr > td > table > tbody > tr > td:nth-child(2) > div > font"); data["last_bill"] = myParseFunction(ifame, "#fixBand5 > table > tbody > tr > td > table > tbody > tr > td:nth-child(4) > div > font"); data["bill_new"] = myParseFunction(ifame, "#fixBand5 > table > tbody > tr > td > table > tbody > tr > td:nth-child(6) > div > font"); data["bill_paid"] = myParseFunction(ifame, "#fixBand5 > table > tbody > tr > td > table > tbody > tr > td:nth-child(5) > div > font"); // data["bill_adjust"] = myParseFunction(ifame, ""); // data["cycle_interest"] = myParseFunction(ifame, ""); return data } function parseJiaoTong(resText){ //var resText = document.documentElement.outerHTML; var ifame = document.createElement("ifame"); ifame.innerHTML = resText; var data = {}; data['bank'] = '交通银行'; data['username'] = resText.match2(/尊敬的 (S+)您好!</p>/); data["bill_month"] = resText.match2(/以下是您(S+?月)份的信用卡电子账单。/); data["bill_peroid"] = resText.match2(/账单周期:(.*?)</p>/); data["due_date"] = myParseFunction(ifame,'#billInfo1 > table > tbody > tr:nth-child(1) > td:nth-child(2)'); data["need_pay"] = myParseFunction(ifame, '#billInfo1 > table > tbody > tr:nth-child(2) > td:nth-child(2)'); data["min_need_pay"]= myParseFunction(ifame, '#billInfo1 > table > tbody > tr:nth-child(3) > td:nth-child(2)'); data["card_limit"] = myParseFunction(ifame, '#billInfo1 > table > tbody > tr:nth-child(4) > td:nth-child(2)'); data["cash_limit"] = myParseFunction(ifame, '#billInfo1 > table > tbody > tr:nth-child(5) > td:nth-child(2)'); data["card_number"] = resText.match2(/卡号:(S+?)</p>/); data['last_bill'] = myParseFunction(ifame, '#container > div:nth-child(6) > table > tbody > tr:nth-child(2) > td:nth-child(3)'); data['bill_paid'] = myParseFunction(ifame, '#container > div:nth-child(6) > table > tbody > tr:nth-child(2) > td:nth-child(4)'); data['bill_new'] = myParseFunction(ifame, '#container > div:nth-child(6) > table > tbody > tr:nth-child(2) > td:nth-child(5)'); data['avb_point'] = myParseFunction(ifame, '#billInfo20 > table > tbody > tr:nth-child(2) > td:nth-child(2)'); return data; } function parseJianShe(resText){ //var resText = document.documentElement.outerHTML; var ifame = document.createElement("ifame"); ifame.innerHTML = resText; var data = {}; data['bank'] = '建设银行'; data['username'] = resText.match2(/尊敬的(.*?),您好!</font>/); data["bill_month"] = resText.match2(/<br>至(.*?月).*?日</font></b>信用卡账户变动情/); //data["bill_peroid"] = resText.match2(/sans-serif">(.*?)<br>至.*?</font>/) + resText.match2(/sans-serif">.*?<br>(至.*?)</font>/); data["bill_peroid"] = resText.match2(/(d+年d+月d+日)<br>至d+年d+月d+日/) + resText.match2(/d+年d+月d+日<br>(至d+年d+月d+日)/); data["due_date"] = myParseFunction(ifame,'td[width="34%"] > table > tbody > tr:nth-child(3) > td:nth-child(2) > font > b'); data["need_pay"] = myParseFunction(ifame, 'td[width="62%"] > table > tbody > tr:nth-child(3) > td:nth-child(2) > font > b'); data["min_need_pay"]= myParseFunction(ifame, 'td[width="62%"] > table > tbody > tr:nth-child(3) > td:nth-child(3) > font'); data["card_limit"] = myParseFunction(ifame, 'td[width="34%"] > table > tbody > tr:nth-child(4) > td:nth-child(2) > font > a').trim(); data["cash_limit"] = myParseFunction(ifame,'td[width="34%"] > table > tbody > tr:nth-child(5) > td:nth-child(2) > font'); //data["card_number"] = resText.match2(/卡号:(S+?)</p>/); data['last_bill'] = myParseFunction(ifame, 'table[width="875"] > tbody > tr:nth-child(4) > td:nth-child(3) > font'); //data['bill_paid'] = myParseFunction(ifame, "#table4 > tbody > tr:nth-child(2) > td:nth-child(4)"); //data['bill_new'] = myParseFunction(ifame, "#table4 > tbody > tr:nth-child(2) > td:nth-child(5)"); //data['avb_point'] = myParseFunction(ifame, "#table5 > tbody > tr:nth-child(2) > td:nth-child(2)"); return data; } // 定义解析函数映射字典 var MAP_PARSER_FUNCTION = { "zhaoshang": parseZhaoShang, "guangfa": parseGuangFa, "pingan": parsePingAn, "zhongxin": parseZhongXin, "jiaotong": parseJiaoTong, "jianshe": parseJianShe }; var datas = []; function extractBank(keyword){ searchMail(keyword); console.debug(keyword+ '的mids是:'); console.debug(midObj[keyword]); for (var indexi=0;indexi< midObj[keyword].length; indexi++){ var read_res = readMail(midObj[keyword][indexi]); var data = MAP_PARSER_FUNCTION[keyword](read_res); datas.push(data); console.debug('提取的 ' + KEYWORD_DICK[keyword] + ' 单个信息是:'); console.debug(data); } } function runAll(){ for (var key in KEYWORD_DICK){ extractBank(key); } console.debug('完成提取'); console.debug(datas); console.debug(JSON.stringify(datas)); return JSON.stringify(datas); } //runAll(); // extractBank('guangfa'); // extractBank('jiaotong'); // extractBank('jianshe'); // // console.debug('完成提取'); // console.debug(datas); // // console.debug(JSON.stringify(datas));