«

模拟登录抓取data

emer 发布于 2019-6-10 10:16   2105 次阅读     


var express=require("express");
var app=express();
var request=require("request");
var md5=require('md5');
var qs=require("querystringify");
var token1="";
function reqAction(opts){
var pro=new Promise(function(resolve,reject){
request(opts,(err,res,body)=>{
if(body){
resolve(JSON.parse(body));
}
})
});
return pro;
}
function loginAction(){
var newPro=new Promise(function(resolve,reject){

let user={
user:"user1",
password:md5("user123")
};
let headers={
'User-Agent': `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36`
,"Referer":"http://172.31.30.102:8082/gateway/login.html",
"Host":"172.31.30.102:8082"
}
let str=qs.stringify(user,true);
let opts={
url:"http://172.31.30.102:8082/gateway/sys/login"+str,
headers:headers,
methods:"GET"
}
reqAction(opts).then(result=>{
resolve(result);
}).catch(err=>{
console.log({"err":err})
});

});
return newPro;
}

app.get("/",(req,res,err)=>{
var headers={
'User-Agent': `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36`
,"Referer":"http://172.31.30.102:8082/gateway/home.html",
"Host":"172.31.30.102:8082",
"token":token1
}
var thisOpts={
url:"http://172.31.30.102:8082/gateway/stat/getSysConnData",
headers:headers
}
if(!token1){
loginAction().then(results=>{
console.log(results)
if(results.code==0){
token1=results.data.token;
console.log("............"+token1);
thisOpts.headers.token=token1;
console.log(thisOpts);
reqAction(thisOpts).then(data=>{
console.log("------------------2.5")
res.json(data);
}).catch(err=>{
console.log({"err":err});
})
}
}).catch(err=>console.log(err));
}else{
reqAction(thisOpts).then(data=>{
console.log("------------------2.6")
res.json(data);
}).catch(err=>{
console.log({"err":err});
})
}
})

app.get("/data",(req,res,err)=>{
var thisOpts={
url:"http://172.31.30.102:8082/gateway/service",
method:"POST",
headers:{
'Accept': 'application/json, text/javascript, */*; q=0.01',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
,"Referer":"http://172.31.30.102:8082/gateway/home.html",
'Host':"172.31.30.102:8082",
'Origin':' http://172.31.30.102:8082',
"Content-Type": "application/json",
'token':'8342A376-3CD0-EEEC-423D-BE54CFD91308',
}
}
loginAction().then(results=>{
if(results.code==0){
token1=results.data.token;
console.log("...........aaa."+token1);
// thisOpts.body.header=token1;
var str={"service":"job.stat.getMQConnData","get":{},"post":null,"header":{"token":token1}};
thisOpts.body=JSON.stringify(str);
request(thisOpts, (e, b, d) => {
if (e) return console.log(e);
res.send(d);
})
}
}).catch(err=>console.log(err));
})







app.listen(3005,()=>{
console.log("port 3005");
})