我正在使用这个casperjs nodejs app:read the documentation
我已经让我的代码在本地工作并且在上传到Lambda时,但是我需要将我的代码包装在exports.handler中,这样我就可以从API网关向函数传递数据。 (我已经使用其他未使用此应用程序的函数执行此操作。)当我将我的代码放在exports.handler中时,它会失败。我已经测试过将事件数据添加到lambda中的测试事件中,并且我已尝试对其进行硬编码。当我的代码在exports.handler中时,两者都失败了。为什么exports.handler会破坏这段代码?
没有exports.handler的工作代码:
var ua = 'Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1';
var casper = require('casper').create({
viewportSize: {width: 768, height: 1024},
userAgent: ua//,
//verbose: true,
//logLevel: 'debug'
});
exports.handler = function (event, context) {
var login = {};
var usrObj = {
succ: {},
err: []
};
//exit function
var exit = function() {
setTimeout(function() {
casper.page.close();
casper.exit();
casper.bypass(1);
}, 10);
};
//success & error message function
var message = function(dat) {
if (dat === 'credentials') {
usrObj['err'].push('Error: Login credentials are missing');
}
else if (dat) {
usrObj['err'].push(dat);
}
if (usrObj['err'].length > 0) {
console.log('Error not empty...');
console.log(JSON.stringify(usrObj.err));
context.fail(JSON.stringify(usrObj.err));
return exit();
}
else if (usrObj['succ']) {
console.log('Success not empty...');
console.log(JSON.stringify(usrObj.succ));
context.succeed(JSON.stringify(usrObj.succ));
return exit();
}
};
//trim login credentials
login.email = event.email;
login.pw = event.pw;
if (login.email && login.pw) {
casper.start('https://vimeo.com/log_in');
casper.waitForSelector('form#login_form',
function success() {
this.echo(this.getCurrentUrl());
this.sendKeys('form#login_form input[name="email"]', login.email);
this.sendKeys('form#login_form input[name="password"]', login.pw);
this.click('form#login_form input[type=submit][value="Log in with email"]');
},
function fail() {
message('Error with Vimeo [page not loading]')
}
);
casper.waitForSelector('#page_header>h1>a',
function success() {
this.echo(this.getCurrentUrl());
usrObj['succ']['uname'] = this.getHTML('span.topnav_user_name');
usrObj['succ']['profile'] = this.getElementAttribute('li.topnav_user_profile>a', 'href');
var test = [];
if (!usrObj.succ.uname) {test.push('Username not retrieved')}
if (!usrObj.succ.profile) {test.push('Profile link not retrieved')}
if (test.length > 0) {message(test.join('<br />'));}
//else {message();}
},
function fail() {
message('Login not successful');
}
);
casper.thenOpen('https://vimeo.com/staceydavidgearz',
function success() {
this.echo('Stacey David Profile: ' + this.getTitle());
this.echo(this.getCurrentUrl());
var finish = function() {
usrObj['succ']['foll'] = true;
message();
}
var foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox');
if (foll === '0 0 10 10') {
this.click('button[data-fatal-attraction="container:profile_page|component:follow"]');
setTimeout(function() {
foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox');
if (foll === '0 0 10 10') {
message('Can\'t follow SD');
}
else {finish();}
}, 250);
}
else {
finish();
}
},
function fail() {
message('Not going to Stacey David profile page.');
}
);
casper.run();
}
else {message('credentials');}
};
exports.handler中的代码失败:
[]
Lambda回复:
体
Calling casperJS: /var/task/node_modules/casperjs/bin/casperjs [ '/var/task/casperjs-script.js' ] { PHANTOMJS_EXECUTABLE: '/var/task/phantomjs' }
child process exited with code 1
日志:
Encounter.findOne({ order: 'random()' }).then((encounter) => {
// single random encounter
});
答案 0 :(得分:0)
确保您还使用casper组件上传依赖项“node_modules”。 给出错误是因为在lambda执行中没有在require中找到该组件。
答案 1 :(得分:0)
请点击此链接:https://github.com/narainsagar/node-casperjs-aws-lambda/issues/6#issuecomment-279177650
您好,您可以通过casper args支持将数据传递给scrapper脚本:
在index.js文件中对runCasper函数进行一些更改,如下所示:
var utils = require('utils'); utils.dump(casper.cli.get('email')); utils.dump(casper.cli.get('password')); // OR // utils.dump(casper.cli.raw.get('email')); // utils.dump(casper.cli.raw.get('password'));
在您的casper脚本中,您可以获得这样的参数数据 下面:
{{1}}希望这会有所帮助..
干杯,
纳拉因