Commit d71f794d by 马玮

login but no cookies

parent f13acd3d
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
User: MaWei User: MaWei
Date: 2017.7.31 Date: 2017.7.31
*/ */
'use strict';
const path = require('path'); const path = require('path');
const program = require('commander'); const program = require('commander');
const config = require('config'); const config = require('config');
...@@ -10,17 +11,15 @@ const dealerSpider = require('./lib/dealerspider/index'); ...@@ -10,17 +11,15 @@ const dealerSpider = require('./lib/dealerspider/index');
program program
.version('0.1.0') .version('0.1.0')
.option('-w, --website <website name>', 'Add website we want fetch') .option('-w, --webname <website name>', 'Add website we want fetch')
.option('-a, --action <task action>', 'Add task we want to execute') .option('-t, --task <task name>', 'Add task we want to execute')
.parse(process.argv); .parse(process.argv);
(function() { // console.log(process.pid);
// console.log(process.pid); let dealerWebInfo = config.get('Dealer')[program.webname];
// captcha.decode(); let spider = new dealerSpider({
let spider = dealerSpider.getSpider({ username : dealerWebInfo.username,
'website': program.website password : dealerWebInfo.password,
}); webname : program.webname,
captcha_type = config.get('dealer_account')[program.website]['captcha_type']; task : program.task
captcha.decode(captcha_type, program.website + '.png'); }).execute();
// spider.do(program.action); \ No newline at end of file
}());
\ No newline at end of file
...@@ -5,19 +5,19 @@ host = "http://api.dama2.com:7766/app/" ...@@ -5,19 +5,19 @@ host = "http://api.dama2.com:7766/app/"
user = "test" user = "test"
pwd = "test" pwd = "test"
[Dealer.ifeng] [Dealer.autoifeng]
username = "上海车城汽车" username = "上海车城汽车"
password = "Checheng235#" password = "Checheng235#"
captchaType = "FOUR_CHARACTERS" captchaType = "FOUR_CHARACTERS"
[Dealer.easypass] [Dealer.easypass]
username = "100074110" username = "100074110"
password = "hnmz147852" password = "hnmz147852."
captchaType = "ARITHMETIC" captchaType = "ARITHMETIC"
[Dealer.autohome] [Dealer.autohome]
username = "河南明志汽车大迈8054" username = "河南明志汽车大迈8054"
password = "hnmz147852" password = "hnmz147852."
captchaType = "FOUR_LETTERS" captchaType = "FOUR_LETTERS"
[Captcha.ruokuai] [Captcha.ruokuai]
......
...@@ -3,10 +3,15 @@ const ruokuai = require('./ruokuai'); ...@@ -3,10 +3,15 @@ const ruokuai = require('./ruokuai');
const captcha = exports; const captcha = exports;
captcha.decode = function (captchaType, imageFile, callback) { captcha.decode = function (captchaType, imageFile, callback) {
console.log(captchaType); console.info('captchaType:', captchaType);
console.log(imageFile); console.info('imageFile:', imageFile);
const typeids = config.get('Captcha.ruokuai.typeids'); const typeids = config.get('Captcha.ruokuai.typeids');
typeid = typeids[captchaType]; typeid = typeids[captchaType];
ruokuai.decode(typeid, imageFile, function (captcha) { return new Promise(function (resolve, reject) {
ruokuai.decode(typeid, imageFile).then(function (captchaTxt) {
resolve(captchaTxt);
}).catch(function (error) {
console.error(error);
});
}); });
} }
\ No newline at end of file
...@@ -7,7 +7,7 @@ const request = require('request'); ...@@ -7,7 +7,7 @@ const request = require('request');
const filename = '2.png'; const filename = '2.png';
let ruokuai = exports; let ruokuai = exports;
ruokuai.decode = function (typeid, imageFile, callback) { ruokuai.decode = function (typeid, imageFile) {
let ruokuaiInfo = config.get('Captcha.ruokuai'); let ruokuaiInfo = config.get('Captcha.ruokuai');
const image = fs.createReadStream(imageFile); const image = fs.createReadStream(imageFile);
formData = { formData = {
...@@ -18,12 +18,20 @@ ruokuai.decode = function (typeid, imageFile, callback) { ...@@ -18,12 +18,20 @@ ruokuai.decode = function (typeid, imageFile, callback) {
typeid: typeid, typeid: typeid,
image: fs.createReadStream(imageFile) image: fs.createReadStream(imageFile)
} }
return new Promise(function (resolve, reject) {
request.post({url:'http://api.ruokuai.com/create.json', formData: formData}, function optionalCallback(err, httpResponse, body) { request.post({url:'http://api.ruokuai.com/create.json', formData: formData}, function optionalCallback(err, httpResponse, body) {
if (err) { if (err) {
return console.error('captcha decode failed:', err); reject(new Error(err));
} }
var captcha = JSON.parse(body); try {
console.log(captcha); let captchaData = JSON.parse(body);
if (callback) callback(captcha); captchaTxt = captchaData.Result;
if (captchaTxt) {
resolve(captchaTxt);
}
} catch (e) {
reject(new Error(body));
}
});
}); });
} }
'use strict';
process.env.NODE_CONFIG_DIR = '../config';
const fs = require('fs');
const phantom = require('phantom');
const cheerio = require('cheerio');
const config = require('config');
const captcha = require('../captcha');
class AutoHome {
constructor(params) {
this.name = 'autohome';
this.params = params;
console.log(params);
}
execute() {
return new Promise((resolve, reject) => {
switch (this.params.task) {
case 'login': {
console.log('do login');
this.login();
break;
}
case 'tryLogin': {
console.log('do login');
this.tryLogin();
break;
}
}
});
}
async tryLogin() {
const instance = await phantom.create(['--ignore-ssl-errors=yes']);
const page = await instance.createPage();
page.property('viewportSize', {width: 1920, height: 1080});
await page.on("onResourceRequested", function(requestData) {
console.info('Requesting', requestData.url);
});
const status = await page.open('http://ics.autohome.com.cn/passport/Home/Index');
console.log(status);
// data:image/png;base64, + baseTxt
await page.render('m5.png');
await instance.exit();
}
async login() {
const username = this.params.username;
const password = this.params.password;
const instance = await phantom.create(['--ignore-ssl-errors=yes']);
const page = await instance.createPage();
page.property('viewportSize', {width: 1920, height: 1080});
await page.on("onResourceRequested", function(requestData) {
console.info('Requesting', requestData.url);
});
const status = await page.open('http://ics.autohome.com.cn/passport/Home/Index');
await console.log(JSON.stringify(page.cookies()));
console.log(status);
console.log(username);
console.log(password);
page.render('m5.png');
const content = await page.property('content');
const $ = cheerio.load(content);
console.log('倒霉');
// data:image/png;base64, + baseTxt
page.property('clipRect', { top: 313, left: 1430, width: 64, height: 27 });
await page.renderBase64('PNG').then(base64text => {
const imageFile = 'tmp/' + (new Date) . getTime() + '.gif';
fs.writeFileSync(imageFile, base64text, 'base64');
const captchaType = config.get('Dealer')[this.name]['captchaType'];
captcha.decode(captchaType, imageFile).then((captchaTxt) => {
console.info('imageFile:', imageFile);
console.info('captchaTxt:', captchaTxt);
page.evaluate(function(username, password, captchaTxt) {
$('#UserNameDealer').val(username);
$('#PasswordDealer').val(password);
$('#checkCodeDealer').val(captchaTxt);
$('#btnDealer').trigger('click');
}, username, password, captchaTxt);
page.property('clipRect', { top: 0, left: 0, width: 1920, height: 1080 });
(async function() {
console.log('kkkkkkkkkk');
setTimeout(function(){
page.render('m7.png');
console.log(JSON.stringify(page.cookies()));
}, 5000);
})();
// await console.log(JSON.stringify(page.cookies()));
// await instance.exit();
}).catch((error) => {
console.error(error);
});
});
}
}
module.exports = AutoHome;
\ No newline at end of file
'use strict';
const phantom = require('phantom');
const cheerio = require('cheerio');
class AutoIfeng {
constructor(params) {
this.params = params;
console.log(params);
}
execute() {
return new Promise((resolve, reject) => {
switch (this.params.task) {
case 'login': {
console.log('do login');
this.login();
break;
}
case 'tryLogin': {
break;
}
}
});
}
async login() {
const passport = this.params.username;
const password = this.params.password;
const instance = await phantom.create(['--ignore-ssl-errors=yes']);
const page = await instance.createPage();
page.property('viewportSize', {width: 1920, height: 1080});
await page.on("onResourceRequested", function(requestData) {
console.info('Requesting', requestData.url)
});
const status = await page.open('http://dealer.auto.ifeng.com/dealerback/dealerIndexPage');
console.log(status);
console.log(passport);
page.evaluate(function(passport, password) {
document.querySelector('input[id=passport]').value = passport;
document.querySelector('input[id=password]').value = password;
}).then(function(html) {
console.log(html);
});
const content = await page.property('content');
const $ = cheerio.load(content)
console.log('倒霉');
await page.render('m2.png');
await instance.exit();
}
}
module.exports = AutoIfeng;
\ No newline at end of file
'use strict';
const phantom = require('phantom');
const cheerio = require('cheerio');
class EasyPass {
constructor() {
}
}
module.exports = EasyPass;
\ No newline at end of file
let dealerspider = exports; 'use strict';
const AutoIfeng = require('./autoifeng');
const AutoHome = require('./autohome');
const EasyPass = require('./easypass');
dealerspider.getSpider = function (data) { class DealerSpider {
console.log(data.website); constructor(params) {
return this; let self = this;
} let _params = {};
this.params = Object.assign(_params, params);
this.spider = this.getSpider();
}
dealerspider.do = function (action) { getSpider() {
console.log('I want ' + action); let spider;
} switch (this.params.webname) {
case 'autoifeng': {
spider = new AutoIfeng(this.params);
break;
}
case 'autohome': {
spider = new AutoHome(this.params);
break;
}
case 'easypass': {
spider = new EasyPass(this.params);
break;
}
}
if (spider) {
return spider;
} else {
throw new Error('Spider not found');
}
}
dealerspider.login = function() { execute() {
console.log('login'); this.spider.execute();
}
} }
dealerspider.trylogin = function() { module.exports = DealerSpider;
console.log('trylogin');
}
...@@ -13,8 +13,10 @@ program ...@@ -13,8 +13,10 @@ program
.option('-w, --website <website name>', 'Add website we want fetch') .option('-w, --website <website name>', 'Add website we want fetch')
.parse(process.argv); .parse(process.argv);
(function() { const captchaType = config.get('Dealer')[program.website]['captchaType'];
captcha_type = config.get('Dealer')[program.website]['captchaType']; const imageFile = 'images/' + program.website + '.png';
const imagefile = 'images/' + program.website + '.png'; captcha.decode(captchaType, imageFile).then((captchaTxt) => {
captcha.decode(captcha_type, imagefile); console.info('captchaTxt:', captchaTxt);
}()); }).catch((error) => {
\ No newline at end of file console.error(error);
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment