我正在抓取一个网站使用PHP,我能够登录到网站和抓取网站的数据。现在我已经切换到Casper js,但它不允许我登录站点。也尝试使用不同的用户代理和IP,但不能获得任何成功。
casper = require('casper').create();
casper.on('started', function () {
this.page.customHeaders = {
"User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.8",
}
});
casper.start('https:somesite.com');
casper.then(function() {
this.fill('form#J_Form', {
'TPL_password': '123',
'TPL_username': 'xyz',
}, true);
});
casper.wait(20000, function() {
this.echo("I've waited for a 2 seconds.");
});
casper.then(function() {
casper.capture('Screeenshots/loginsuccessfully1.png');
});
casper.thenOpen('https://item.othersite.com/item.htm?id=538450584178', function() {
this.echo(this.getHTML());
});
casper.run(function() {
this.echo('login successfully').exit();
});任何建议都将是有帮助的,谢谢。
发布于 2018-02-20 22:38:04
首先,我建议使用这个来启动你的Casper:
casper = require('casper').create({
verbose: true,
logLevel: 'debug',
viewportSize: {width: 1280, height: 800},
});因为它将为您提供有关该过程中实际发生的情况的更多信息。
为了回答你的问题,在login.tmall.com,登录是在iframe中,所以你需要首先切换到那个iframe来填写表单。你可以用这个实现这一点。
casper.then(function() {
this.withFrame(0, function() {
this.fill('form#J_Form', {
'TPL_password': '123',
'TPL_username': 'xyz',
}, true);
});
});然后将其与您的代码相结合,您将得到以下代码
casper = require('casper').create({
verbose: true,
logLevel: 'debug',
viewportSize: {width: 1280, height: 800},
});
casper.on('started', function () {
this.page.customHeaders = {
"User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.8",
}
});
casper.start('https://login.tmall.com/', function() {
this.capture('test.png');
});
casper.then(function() {
this.withFrame(0, function() {
this.fill('form#J_Form', {
'TPL_password': '123',
'TPL_username': 'xyz',
}, true);
});
});
casper.wait(20000, function() {
this.echo("I've waited for a 2 seconds.");
});
casper.then(function() {
casper.capture('Screeenshots/loginsuccessfully1.png');
});
casper.thenOpen('https://item.othersite.com/item.htm?id=538450584178', function() {
this.echo(this.getHTML());
});
casper.run(function() {
this.echo('login successfully').exit();
});https://stackoverflow.com/questions/48861458
复制相似问题