Java模拟新浪微博登陆抓取数据
程序员文章站
2024-03-06 14:08:02
前言:
兄弟们来了来了,最近有人在问如何模拟新浪微博登陆抓取数据,我听后默默地抽了一口老烟,暗暗的对自己说,老汉是时候该你出场了,所以今天有时间就整理整理,浅谈一二。...
前言:
兄弟们来了来了,最近有人在问如何模拟新浪微博登陆抓取数据,我听后默默地抽了一口老烟,暗暗的对自己说,老汉是时候该你出场了,所以今天有时间就整理整理,浅谈一二。
首先:
要想登陆新浪微博需要预登陆,即是将账号base64加密,密码rsa加密以及请求链接获取一些登陆需要参数,返回的接送字符串如:
{"retcode":0,"servertime":1487292003,"pcid":"gz-9e1f24c9acdefb111e1c8078558c7d9c0bf2","nonce":"vhrdg1","pubkey":"eb2a38568661887fa180bddb5cabd5f21c7bfd59c090cb2d245a87ac253062882729293e5506350508e7f9aa3bb77f4333231490f915f6d63c55fe2f08a49b353f444ad3993cacc02db784abbb8e42a9b1bbfffb38be18d78e87a0e41b9b8f73a928ee0ccee1f6739884b9777e4fe9e88a1bbe495927ac4a799b3181d6442443","rsakv":"1330428213","is_openlock":0,"lm":1,"smsurl":"https:\/\/login.sina.com.cn\/sso\/msglogin?entry=weibo&mobile=18360903574&s=ea7a2e91c5f1d6da7f42aa87fe6963d0","showpin":0,"exectime":222}
,接下来是预登陆处理代码:
/** * @author longjin * @description 初始登录信息<br> 返回false说明初始失败 * @return */ public boolean prelogin(){ boolean flag = false; try { su = new string(base64.encodebase64(urlencoder.encode(username, "utf-8").getbytes())); string url = "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&rsakt=mod&checkpin=1&" + "client=ssologin.js(v1.4.5)&_=" + gettimestamp(); url += "&su=" + su; string content; content = httputils.getrequest(client, url); system.out.println("content------------" + content); jsonobject json = jsonobject.fromobject(content); system.out.println(json); servertime = json.getlong("servertime"); nonce = json.getstring("nonce"); rsakv = json.getstring("rsakv"); pubkey = json.getstring("pubkey"); flag = encodepwd(); } catch (unsupportedencodingexception e) { system.out.println("抛出unsupportedencoding异常"); } catch (clientprotocolexception e) { system.out.println("抛出clientprotocol异常"); } catch (ioexception e) { system.out.println("抛出io异常"); } return flag; }
其次:
获取登陆需要的参数后使用post请求,将上述预登陆后处理数据作为参数代入请求,得到结果如下:
<html> <head> <meta http-equiv="content-type" content="text/html; charset=gbk" /> <title>新浪通行证</title> <script charset="utf-8" src="http://i.sso.sina.com.cn/js/ssologin.js"></script> </head> <body> 正在登录 ... <script> try{sinassocontroller.setcrossdomainurllist({"retcode":0,"arrurl":["http:\/\/passport.97973.com\/sso\/crossdomain?action=login&savestate=1518828005","http:\/\/passport.weibo.cn\/sso\/crossdomain?action=login&savestate=1"]});} catch(e){ var msg = e.message; var img = new image(); var type = 1; img.src = 'http://login.sina.com.cn/sso/debuglog?msg=' + msg +'&type=' + type; }try{sinassocontroller.crossdomainaction('login',function(){location.replace('http://passport.weibo.com/wbsso/login?ssosavestate=1518828005&url=http%3a%2f%2fweibo.com%2fajaxlogin.php%3fframelogin%3d1%26callback%3dparent.sinassocontroller.feedbackurlcallback&ticket=st-ntuwodg3mjkxmq==-1487292005-gz-ff56c545999f864fc6c7ab86fca9fa4a-1&retcode=0');});} catch(e){ var msg = e.message; var img = new image(); var type = 2; img.src = 'http://login.sina.com.cn/sso/debuglog?msg=' + msg +'&type=' + type; } </script> </body> </html>
然后用正则截取其中我们想要的部分:location.replace('')中间部分,正则表达式为:
string regex = "location.replace\\('([\\s\\s]*?)'\\);";
将正则得到的结果进行处理,如果成功则使用get请求得到的链接,登陆部分的代码如下:
/** * @author longjin * @description 登录 * @return true:登录成功 */ public boolean login() { if(prelogin()) { string url = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)"; list<namevaluepair> parms = new arraylist<namevaluepair>(); parms.add(new basicnamevaluepair("entry", "weibo")); parms.add(new basicnamevaluepair("geteway", "1")); parms.add(new basicnamevaluepair("from", "")); parms.add(new basicnamevaluepair("savestate", "7")); parms.add(new basicnamevaluepair("useticket", "1")); parms.add(new basicnamevaluepair("pagerefer", "http://login.sina.com.cn/sso/logout.php?entry=miniblog&r=http%3a%2f%2fweibo.com%2flogout.php%3fbackurl%3d%2f")); parms.add(new basicnamevaluepair("vsnf", "1")); parms.add(new basicnamevaluepair("su", su)); parms.add(new basicnamevaluepair("service", "miniblog")); parms.add(new basicnamevaluepair("servertime", servertime + "")); parms.add(new basicnamevaluepair("nonce", nonce)); parms.add(new basicnamevaluepair("pwencode", "rsa2")); parms.add(new basicnamevaluepair("rsakv", rsakv)); parms.add(new basicnamevaluepair("sp", sp)); parms.add(new basicnamevaluepair("encoding", "utf-8")); parms.add(new basicnamevaluepair("prelt", "182")); parms.add(new basicnamevaluepair("url", "http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinassocontroller.feedbackurlcallback")); parms.add(new basicnamevaluepair("domain", "sina.com.cn")); parms.add(new basicnamevaluepair("returntype", "meta")); try { string content = httputils.postrequest(client, url, parms); system.out.println("content----------" + content); string regex = "location.replace\\('([\\s\\s]*?)'\\);";//\\(' '\\)特殊符转译 匹配('')里面的内容//location.replace([\\s\\s]*?) pattern p = pattern.compile(regex); matcher m = p.matcher(content); if(m.find()) { system.out.println("ss = "+m.group()); location = m.group(1); if(location.contains("reason=")) {//如果你走进了这一步,恭喜报错了 errinfo = location.substring(location.indexof("reason=") + 7); errinfo = urldecoder.decode(errinfo, "gbk"); } else { system.out.println("location = "+location); string result = httputils.getrequest(client, location);//.substring(2, location.length()-2) int beginindex = result.indexof("("); int endindex = result.lastindexof(")"); result = result.substring(beginindex+1, endindex);//截取括号里面的json字符串 //content = urldecoder.decode(content, "utf-8"); jsonobject jsonobject = jsonobject.fromobject(result);//转换为json //获取uniqueid+userdomain用于访问时带的参数 uniqueid = jsonobject.getjsonobject("userinfo").getstring("uniqueid"); userdomain = jsonobject.getjsonobject("userinfo").getstring("userdomain"); system.out.println("result--------------" + result); return true; } } } catch (clientprotocolexception e) { system.out.println("抛出clientprotocol异常"); } catch (ioexception e) { system.out.println("抛出io异常"); } } return false; }
补充一下密码加密部分的代码:
private static string sina_js = "var sinassoencoder=sinassoencoder||{};(function(){var hexcase=0;var chrsz=8;this.hex_sha1=function(s){return binb2hex(core_sha1(str2binb(s),s.length*chrsz));};var core_sha1=function(x,len){x[len>>5]|=0x80<<(24-len%32);x[((len+64>>9)<<4)+15]=len;var w=array(80);var a=1732584193;var b=-271733879;var c=-1732584194;var d=271733878;var e=-1009589776;for(var i=0;i<x.length;i+=16){var olda=a;var oldb=b;var oldc=c;var oldd=d;var olde=e;for(var j=0;j<80;j++){if(j<16)w[j]=x[i+j];else w[j]=rol(w[j-3]^w[j-8]^w[j-14]^w[j-16],1);var t=safe_add(safe_add(rol(a,5),sha1_ft(j,b,c,d)),safe_add(safe_add(e,w[j]),sha1_kt(j)));e=d;d=c;c=rol(b,30);b=a;a=t;}a=safe_add(a,olda);b=safe_add(b,oldb);c=safe_add(c,oldc);d=safe_add(d,oldd);e=safe_add(e,olde);}return array(a,b,c,d,e);};var sha1_ft=function(t,b,c,d){if(t<20)return(b&c)|((~b)&d);if(t<40)return b^c^d;if(t<60)return(b&c)|(b&d)|(c&d);return b^c^d;};var sha1_kt=function(t){return(t<20)?1518500249:(t<40)?1859775393:(t<60)?-1894007588:-899497514;};var safe_add=function(x,y){var lsw=(x&0xffff)+(y&0xffff);var msw=(x>>16)+(y>>16)+(lsw>>16);return(msw<<16)|(lsw&0xffff);};var rol=function(num,cnt){return(num<<cnt)|(num>>>(32-cnt));};var str2binb=function(str){var bin=array();var mask=(1<<chrsz)-1;for(var i=0;i<str.length*chrsz;i+=chrsz)bin[i>>5]|=(str.charcodeat(i/chrsz)&mask)<<(24-i%32);return bin;};var binb2hex=function(binarray){var hex_tab=hexcase?'0123456789abcdef':'0123456789abcdef';var str='';for(var i=0;i<binarray.length*4;i++){str+=hex_tab.charat((binarray[i>>2]>>((3-i%4)*8+4))&0xf)+hex_tab.charat((binarray[i>>2]>>((3-i%4)*8))&0xf);}return str;};this.base64={encode:function(input){input=''+input;if(input=='')return '';var output='';var chr1,chr2,chr3='';var enc1,enc2,enc3,enc4='';var i=0;do{chr1=input.charcodeat(i++);chr2=input.charcodeat(i++);chr3=input.charcodeat(i++);enc1=chr1>>2;enc2=((chr1&3)<<4)|(chr2>>4);enc3=((chr2&15)<<2)|(chr3>>6);enc4=chr3&63;if(isnan(chr2)){enc3=enc4=64;}else if(isnan(chr3)){enc4=64;}output=output+this._keys.charat(enc1)+this._keys.charat(enc2)+this._keys.charat(enc3)+this._keys.charat(enc4);chr1=chr2=chr3='';enc1=enc2=enc3=enc4='';}while(i<input.length);return output;},_keys:'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/='};}).call(sinassoencoder);;(function(){var dbits;var canary=0xdeadbeefcafe;var j_lm=((canary&0xffffff)==0xefcafe);function biginteger(a,b,c){if(a!=null)if('number'==typeof a)this.fromnumber(a,b,c);else if(b==null && 'string' !=typeof a)this.fromstring(a,256);else this.fromstring(a,b);}function nbi(){return new biginteger(null);}function am1(i,x,w,j,c,n){while(--n>=0){var v=x*this[i++]+w[j]+c;c=math.floor(v/0x4000000);w[j++]=v&0x3ffffff;}return c;}function am2(i,x,w,j,c,n){var xl=x&0x7fff,xh=x>>15;while(--n>=0){var l=this[i]&0x7fff;var h=this[i++]>>15;var m=xh*l+h*xl;l=xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);c=(l>>>30)+(m>>>15)+xh*h+(c>>>30);w[j++]=l&0x3fffffff;}return c;}function am3(i,x,w,j,c,n){var xl=x&0x3fff,xh=x>>14;while(--n>=0){var l=this[i]&0x3fff;var h=this[i++]>>14;var m=xh*l+h*xl;l=xl*l+((m&0x3fff)<<14)+w[j]+c;c=(l>>28)+(m>>14)+xh*h;w[j++]=l&0xfffffff;}return c;}biginteger.prototype.am=am3;dbits=28;biginteger.prototype.db=dbits;biginteger.prototype.dm=((1<<dbits)-1);biginteger.prototype.dv=(1<<dbits);var bi_fp=52;biginteger.prototype.fv=math.pow(2,bi_fp);biginteger.prototype.f1=bi_fp-dbits;biginteger.prototype.f2=2*dbits-bi_fp;var bi_rm='0123456789abcdefghijklmnopqrstuvwxyz';var bi_rc=new array();var rr,vv;rr='0'.charcodeat(0);for(vv=0;vv<=9;++vv)bi_rc[rr++]=vv;rr='a'.charcodeat(0);for(vv=10;vv<36;++vv)bi_rc[rr++]=vv;rr='a'.charcodeat(0);for(vv=10;vv<36;++vv)bi_rc[rr++]=vv;function int2char(n){return bi_rm.charat(n);}function intat(s,i){var c=bi_rc[s.charcodeat(i)];return(c==null)?-1:c;}function bnpcopyto(r){for(var i=this.t-1;i>=0;--i)r[i]=this[i];r.t=this.t;r.s=this.s;}function bnpfromint(x){this.t=1;this.s=(x<0)?-1:0;if(x>0)this[0]=x;else if(x<-1)this[0]=x+dv;else this.t=0;}function nbv(i){var r=nbi();r.fromint(i);return r;}function bnpfromstring(s,b){var k;if(b==16)k=4;else if(b==8)k=3;else if(b==256)k=8;else if(b==2)k=1;else if(b==32)k=5;else if(b==4)k=2;else{this.fromradix(s,b);return;}this.t=0;this.s=0;var i=s.length,mi=false,sh=0;while(--i>=0){var x=(k==8)?s[i]&0xff:intat(s,i);if(x<0){if(s.charat(i)=='-')mi=true;continue;}mi=false;if(sh==0)this[this.t++]=x;else if(sh+k>this.db){this[this.t-1]|=(x&((1<<(this.db-sh))-1))<<sh;this[this.t++]=(x>>(this.db-sh));}else this[this.t-1]|=x<<sh;sh+=k;if(sh>=this.db)sh-=this.db;}if(k==8&&(s[0]&0x80)!=0){this.s=-1;if(sh>0)this[this.t-1]|=((1<<(this.db-sh))-1)<<sh;}this.clamp();if(mi)biginteger.zero.subto(this,this);}function bnpclamp(){var c=this.s&this.dm;while(this.t>0&&this[this.t-1]==c)--this.t;}function bntostring(b){if(this.s<0)return '-'+this.negate().tostring(b);var k;if(b==16)k=4;else if(b==8)k=3;else if(b==2)k=1;else if(b==32)k=5;else if(b==4)k=2;else return this.toradix(b);var km=(1<<k)-1,d,m=false,r='',i=this.t;var p=this.db-(i*this.db)%k;if(i-->0){if(p<this.db&&(d=this[i]>>p)>0){m=true;r=int2char(d);}while(i>=0){if(p<k){d=(this[i]&((1<<p)-1))<<(k-p);d|=this[--i]>>(p+=this.db-k);}else{d=(this[i]>>(p-=k))&km;if(p<=0){p+=this.db;--i;}}if(d>0)m=true;if(m)r+=int2char(d);}}return m?r:'0';}function bnnegate(){var r=nbi();biginteger.zero.subto(this,r);return r;}function bnabs(){return(this.s<0)?this.negate():this;}function bncompareto(a){var r=this.s-a.s;if(r!=0)return r;var i=this.t;r=i-a.t;if(r!=0)return r;while(--i>=0)if((r=this[i]-a[i])!=0)return r;return 0;}function nbits(x){var r=1,t;if((t=x>>>16)!=0){x=t;r+=16;}if((t=x>>8)!=0){x=t;r+=8;}if((t=x>>4)!=0){x=t;r+=4;}if((t=x>>2)!=0){x=t;r+=2;}if((t=x>>1)!=0){x=t;r+=1;}return r;}function bnbitlength(){if(this.t<=0)return 0;return this.db*(this.t-1)+nbits(this[this.t-1]^(this.s&this.dm));}function bnpdlshiftto(n,r){var i;for(i=this.t-1;i>=0;--i)r[i+n]=this[i];for(i=n-1;i>=0;--i)r[i]=0;r.t=this.t+n;r.s=this.s;}function bnpdrshiftto(n,r){for(var i=n;i<this.t;++i)r[i-n]=this[i];r.t=math.max(this.t-n,0);r.s=this.s;}function bnplshiftto(n,r){var bs=n%this.db;var cbs=this.db-bs;var bm=(1<<cbs)-1;var ds=math.floor(n/this.db),c=(this.s<<bs)&this.dm,i;for(i=this.t-1;i>=0;--i){r[i+ds+1]=(this[i]>>cbs)|c;c=(this[i]&bm)<<bs;}for(i=ds-1;i>=0;--i)r[i]=0;r[ds]=c;r.t=this.t+ds+1;r.s=this.s;r.clamp();}function bnprshiftto(n,r){r.s=this.s;var ds=math.floor(n/this.db);if(ds>=this.t){r.t=0;return;}var bs=n%this.db;var cbs=this.db-bs;var bm=(1<<bs)-1;r[0]=this[ds]>>bs;for(var i=ds+1;i<this.t;++i){r[i-ds-1]|=(this[i]&bm)<<cbs;r[i-ds]=this[i]>>bs;}if(bs>0)r[this.t-ds-1]|=(this.s&bm)<<cbs;r.t=this.t-ds;r.clamp();}function bnpsubto(a,r){var i=0,c=0,m=math.min(a.t,this.t);while(i<m){c+=this[i]-a[i];r[i++]=c&this.dm;c>>=this.db;}if(a.t<this.t){c-=a.s;while(i<this.t){c+=this[i];r[i++]=c&this.dm;c>>=this.db;}c+=this.s;}else{c+=this.s;while(i<a.t){c-=a[i];r[i++]=c&this.dm;c>>=this.db;}c-=a.s;}r.s=(c<0)?-1:0;if(c<-1)r[i++]=this.dv+c;else if(c>0)r[i++]=c;r.t=i;r.clamp();}function bnpmultiplyto(a,r){var x=this.abs(),y=a.abs();var i=x.t;r.t=i+y.t;while(--i>=0)r[i]=0;for(i=0;i<y.t;++i)r[i+x.t]=x.am(0,y[i],r,i,0,x.t);r.s=0;r.clamp();if(this.s!=a.s)biginteger.zero.subto(r,r);}function bnpsquareto(r){var x=this.abs();var i=r.t=2*x.t;while(--i>=0)r[i]=0;for(i=0;i<x.t-1;++i){var c=x.am(i,x[i],r,2*i,0,1);if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1))>=x.dv){r[i+x.t]-=x.dv;r[i+x.t+1]=1;}}if(r.t>0)r[r.t-1]+=x.am(i,x[i],r,2*i,0,1);r.s=0;r.clamp();}function bnpdivremto(m,q,r){var pm=m.abs();if(pm.t<=0)return;var pt=this.abs();if(pt.t<pm.t){if(q!=null)q.fromint(0);if(r!=null)this.copyto(r);return;}if(r==null)r=nbi();var y=nbi(),ts=this.s,ms=m.s;var nsh=this.db-nbits(pm[pm.t-1]);if(nsh>0){pm.lshiftto(nsh,y);pt.lshiftto(nsh,r);}else{pm.copyto(y);pt.copyto(r);}var ys=y.t;var y0=y[ys-1];if(y0==0)return;var yt=y0*(1<<this.f1)+((ys>1)?y[ys-2]>>this.f2:0);var d1=this.fv/yt,d2=(1<<this.f1)/yt,e=1<<this.f2;var i=r.t,j=i-ys,t=(q==null)?nbi():q;y.dlshiftto(j,t);if(r.compareto(t)>=0){r[r.t++]=1;r.subto(t,r);}biginteger.one.dlshiftto(ys,t);t.subto(y,y);while(y.t<ys)y[y.t++]=0;while(--j>=0){var qd=(r[--i]==y0)?this.dm:math.floor(r[i]*d1+(r[i-1]+e)*d2);if((r[i]+=y.am(0,qd,r,j,0,ys))<qd){y.dlshiftto(j,t);r.subto(t,r);while(r[i]<--qd)r.subto(t,r);}}if(q!=null){r.drshiftto(ys,q);if(ts!=ms)biginteger.zero.subto(q,q);}r.t=ys;r.clamp();if(nsh>0)r.rshiftto(nsh,r);if(ts<0)biginteger.zero.subto(r,r);}function bnmod(a){var r=nbi();this.abs().divremto(a,null,r);if(this.s<0&&r.compareto(biginteger.zero)>0)a.subto(r,r);return r;}function classic(m){this.m=m;}function cconvert(x){if(x.s<0||x.compareto(this.m)>=0)return x.mod(this.m);else return x;}function crevert(x){return x;}function creduce(x){x.divremto(this.m,null,x);}function cmulto(x,y,r){x.multiplyto(y,r);this.reduce(r);}function csqrto(x,r){x.squareto(r);this.reduce(r);}classic.prototype.convert=cconvert;classic.prototype.revert=crevert;classic.prototype.reduce=creduce;classic.prototype.multo=cmulto;classic.prototype.sqrto=csqrto;function bnpinvdigit(){if(this.t<1)return 0;var x=this[0];if((x&1)==0)return 0;var y=x&3;y=(y*(2-(x&0xf)*y))&0xf;y=(y*(2-(x&0xff)*y))&0xff;y=(y*(2-(((x&0xffff)*y)&0xffff)))&0xffff;y=(y*(2-x*y%this.dv))%this.dv;return(y>0)?this.dv-y:-y;}function montgomery(m){this.m=m;this.mp=m.invdigit();this.mpl=this.mp&0x7fff;this.mph=this.mp>>15;this.um=(1<<(m.db-15))-1;this.mt2=2*m.t;}function montconvert(x){var r=nbi();x.abs().dlshiftto(this.m.t,r);r.divremto(this.m,null,r);if(x.s<0&&r.compareto(biginteger.zero)>0)this.m.subto(r,r);return r;}function montrevert(x){var r=nbi();x.copyto(r);this.reduce(r);return r;}function montreduce(x){while(x.t<=this.mt2)x[x.t++]=0;for(var i=0;i<this.m.t;++i){var j=x[i]&0x7fff;var u0=(j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.dm;j=i+this.m.t;x[j]+=this.m.am(0,u0,x,i,0,this.m.t);while(x[j]>=x.dv){x[j]-=x.dv;x[++j]++;}}x.clamp();x.drshiftto(this.m.t,x);if(x.compareto(this.m)>=0)x.subto(this.m,x);}function montsqrto(x,r){x.squareto(r);this.reduce(r);}function montmulto(x,y,r){x.multiplyto(y,r);this.reduce(r);}montgomery.prototype.convert=montconvert;montgomery.prototype.revert=montrevert;montgomery.prototype.reduce=montreduce;montgomery.prototype.multo=montmulto;montgomery.prototype.sqrto=montsqrto;function bnpiseven(){return((this.t>0)?(this[0]&1):this.s)==0;}function bnpexp(e,z){if(e>0xffffffff||e<1)return biginteger.one;var r=nbi(),r2=nbi(),g=z.convert(this),i=nbits(e)-1;g.copyto(r);while(--i>=0){z.sqrto(r,r2);if((e&(1<<i))>0)z.multo(r2,g,r);else{var t=r;r=r2;r2=t;}}return z.revert(r);}function bnmodpowint(e,m){var z;if(e<256||m.iseven())z=new classic(m);else z=new montgomery(m);return this.exp(e,z);}biginteger.prototype.copyto=bnpcopyto;biginteger.prototype.fromint=bnpfromint;biginteger.prototype.fromstring=bnpfromstring;biginteger.prototype.clamp=bnpclamp;biginteger.prototype.dlshiftto=bnpdlshiftto;biginteger.prototype.drshiftto=bnpdrshiftto;biginteger.prototype.lshiftto=bnplshiftto;biginteger.prototype.rshiftto=bnprshiftto;biginteger.prototype.subto=bnpsubto;biginteger.prototype.multiplyto=bnpmultiplyto;biginteger.prototype.squareto=bnpsquareto;biginteger.prototype.divremto=bnpdivremto;biginteger.prototype.invdigit=bnpinvdigit;biginteger.prototype.iseven=bnpiseven;biginteger.prototype.exp=bnpexp;biginteger.prototype.tostring=bntostring;biginteger.prototype.negate=bnnegate;biginteger.prototype.abs=bnabs;biginteger.prototype.compareto=bncompareto;biginteger.prototype.bitlength=bnbitlength;biginteger.prototype.mod=bnmod;biginteger.prototype.modpowint=bnmodpowint;biginteger.zero=nbv(0);biginteger.one=nbv(1);function arcfour(){this.i=0;this.j=0;this.s=new array();}function arc4init(key){var i,j,t;for(i=0;i<256;++i)this.s[i]=i;j=0;for(i=0;i<256;++i){j=(j+this.s[i]+key[i%key.length])&255;t=this.s[i];this.s[i]=this.s[j];this.s[j]=t;}this.i=0;this.j=0;}function arc4next(){var t;this.i=(this.i+1)&255;this.j=(this.j+this.s[this.i])&255;t=this.s[this.i];this.s[this.i]=this.s[this.j];this.s[this.j]=t;return this.s[(t+this.s[this.i])&255];}arcfour.prototype.init=arc4init;arcfour.prototype.next=arc4next;function prng_newstate(){return new arcfour();}var rng_psize=256;var rng_state;var rng_pool;var rng_pptr;function rng_seed_int(x){rng_pool[rng_pptr++]^=x&255;rng_pool[rng_pptr++]^=(x>>8)&255;rng_pool[rng_pptr++]^=(x>>16)&255;rng_pool[rng_pptr++]^=(x>>24)&255;if(rng_pptr>=rng_psize)rng_pptr-=rng_psize;}function rng_seed_time(){rng_seed_int(new date().gettime());}if(rng_pool==null){rng_pool=new array();rng_pptr=0;var t;while(rng_pptr<rng_psize){t=math.floor(65536*math.random());rng_pool[rng_pptr++]=t>>>8;rng_pool[rng_pptr++]=t&255;}rng_pptr=0;rng_seed_time();}function rng_get_byte(){if(rng_state==null){rng_seed_time();rng_state=prng_newstate();rng_state.init(rng_pool);for(rng_pptr=0;rng_pptr<rng_pool.length;++rng_pptr)rng_pool[rng_pptr]=0;rng_pptr=0;}return rng_state.next();}function rng_get_bytes(ba){var i;for(i=0;i<ba.length;++i)ba[i]=rng_get_byte();}function securerandom(){}securerandom.prototype.nextbytes=rng_get_bytes;function parsebigint(str,r){return new biginteger(str,r);}function linebrk(s,n){var ret='';var i=0;while(i+n<s.length){ret+=s.substring(i,i+n)+'\\n';i+=n;}return ret+s.substring(i,s.length);}function byte2hex(b){if(b<0x10)return '0'+b.tostring(16);else return b.tostring(16);}function pkcs1pad2(s,n){if(n<s.length+11){return null;}var ba=new array();var i=s.length-1;while(i>=0&&n>0){var c=s.charcodeat(i--);if(c<128){ba[--n]=c;}else if((c>127)&&(c<2048)){ba[--n]=(c&63)|128;ba[--n]=(c>>6)|192;}else{ba[--n]=(c&63)|128;ba[--n]=((c>>6)&63)|128;ba[--n]=(c>>12)|224;}}ba[--n]=0;var rng=new securerandom();var x=new array();while(n>2){x[0]=0;while(x[0]==0)rng.nextbytes(x);ba[--n]=x[0];}ba[--n]=2;ba[--n]=0;return new biginteger(ba);}function rsakey(){this.n=null;this.e=0;this.d=null;this.p=null;this.q=null;this.dmp1=null;this.dmq1=null;this.coeff=null;}function rsasetpublic(n,e){if(n!=null&&e!=null&&n.length>0&&e.length>0){this.n=parsebigint(n,16);this.e=parseint(e,16);}else alert('invalid rsa public key');}function rsadopublic(x){return x.modpowint(this.e,this.n);}function rsaencrypt(text){var m=pkcs1pad2(text,(this.n.bitlength()+7)>>3);if(m==null)return null;var c=this.dopublic(m);if(c==null)return null;var h=c.tostring(16);if((h.length&1)==0)return h;else return '0'+h;}rsakey.prototype.dopublic=rsadopublic;rsakey.prototype.setpublic=rsasetpublic;rsakey.prototype.encrypt=rsaencrypt;this.rsakey=rsakey;}).call(sinassoencoder);function getpass(pwd,servicetime,nonce,rsapubkey){var rsakey=new sinassoencoder.rsakey();rsakey.setpublic(rsapubkey,'10001');var password=rsakey.encrypt([servicetime,nonce].join('\\t')+'\\n'+pwd);return password;}";
/** * 密码进行rsa加密<br> * 返回false说明加密失败 * @return */ private boolean encodepwd() { scriptenginemanager sem = new scriptenginemanager(); scriptengine se = sem.getenginebyname("javascript"); try { // 使用js加密密码,rsa,调用js内方法 我这里使用的是字符串 也可以直接放入文件中然后读取,如下面注释部分。 se.eval(sina_js); //调用js内部函数用于加密 if (se instanceof invocable) { invocable iv = (invocable) se; sp = (string) iv.invokefunction("getpass", this.password, this.servertime, this.nonce, this.pubkey); } /* filereader fr = new filereader("e:\\encoder.js"); se.eval(fr); invocable invocableengine = (invocable) se; string callbackvalue = (string) invocableengine.invokefunction("encodepwd", pubkey, servertime, nonce, password); sp = callbackvalue;*/ return true; } catch (scriptexception e) { // todo auto-generated catch block //e.printstacktrace(); } catch (nosuchmethodexception e) { // todo auto-generated catch block //e.printstacktrace(); } errinfo = "密码加密失败!"; return false; } /** * @author longjin * @description 返回错误信息 * @return */ public string geterrinfo() { return errinfo; }
登陆部分就基本完成了。
最后来进行测试登陆抓取数据:
public static void main(string[] args) throws clientprotocolexception, ioexception { sinaweibo weibo = new sinaweibo("**", "***");//账号密码在此就不透露了 if(weibo.login()) { system.out.println("登陆成功!"); inputstream con= httputils.getrequests(client, "http://weibo.com/u/"+uniqueid+userdomain);//请求个人主页获取输入流 string cont = readstreambyencoding(con, "utf-8");//将返回的输入流转换为字符串 string sb =httputils.gettext(cont);//通过jsoup获取text内容部分 //readstreamoutfilebyencoding(sb);也可已将获取的内容写入文件中 } else { system.out.println("登录失败!"); } }
得到的结果为:
text--------------我投给了"易建联" 这个选项。 #本土mvp# 本赛季常规赛最有价值球员(mvp)评选小组由中国篮协新闻委员会成员单位代表、俱乐部推荐的地方媒体代表组成,新浪拥有一票,我们将把粉丝们的意见发给篮协。 r本赛季本土mvp是? ????
到此整个登陆就完成了,望有不足之处多提点。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
推荐阅读
-
[分享]模拟新浪微博自动登陆
-
mysql-java解析 新浪微博Json数据,获取uid和text
-
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
-
Java模拟登录新浪微博
-
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发_PHP教程
-
python模拟新浪微博登陆功能(新浪微博爬虫)
-
新浪微博python爬虫分享(一天可抓取 1300 万条数据),超级无敌
-
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发_PHP教程
-
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
-
php 新浪通行证、新浪微博模拟统一登录 (后台网页抓取版) 2016