[CasperJS] 模拟登陆人人网

  如果你不知道CasperJS是什么请看下面的链接

 casperJS安装

casperJS API


使用CasperJS模拟登陆,模拟输入账号密码,点击登陆按钮登陆网站,而不是使用保存cookie,提交cookie的方式登陆。

  大多数网站的username和password会放在一个form里面,然后submit,人人也不例外,但是有些网站并不会使用上述的方式,such as sina。在新浪的登陆首页没有一个form,所以常规的使用CasperJS的fill()填充提交表单是不行的。原以为使用sendKeys()输入账号密码,然后模拟click会成功登陆新浪微博,但是最后还是失败了(原因应该是微博的防护措施做得比较好吧),但是使用同样的方法模拟登陆人人最后还是成功了!

  代码如下

var casper=require('casper').create();
phantom.outputEncoding="GBK";

casper.start('http://www.renren.com',function(){
	this.echo(this.getTitle());
	this.echo('I am waitting.....');
});
casper.wait(5000,function(){
	this.capture('1.jpg');
	this.echo('capture is done');
	this.echo('input username and password....');
});
casper.then(function(){
	this.sendKeys('form#loginForm input[name="email"]','username',{reset:true});
	this.sendKeys('form#loginForm input[name="password"]','password',{reset:true});
	this.echo('input down');
	this.echo('wait for 5s');
});
casper.wait(5000,function(){
	this.capture('2.jpg');
	this.click('form#loginForm input[type="submit"]');
	this.echo('click is done');
});
casper.wait(5000,function(){
	this.capture('3.jpg');
	this.echo('Over');
});
casper.run();
登陆过程中分别截取了三张图片,分别是登陆首页,输入账号密码,点击登陆成功后的图片,因为网速的原因,让casper等一等~

使用sendkeys()比较啰嗦,像人人这种网站也可以直接使用fill()进行模拟登陆

代码如下:

var casper=require('casper').create();
phantom.outputEncoding="GBK";

casper.start('http://www.renren.com',function(){
	this.echo(this.getTitle());
	this.echo('I am waitting.....');
});
casper.wait(5000,function(){
	this.capture('1.jpg');
	this.echo('capture is done');
	this.echo('input username and password....submit');
});
casper.then(function(){
	this.fill('form#loginForm',{
		'email':'username',
		'password':'password'
	},true)
	this.echo('input down');
	this.echo('wait for 5s');
});
casper.wait(5000,function(){
	this.capture('2.jpg');
	this.echo('over');
});
casper.run();
同样是截取了图片,分别是登陆前和登陆后的页面。

尝试了很多次都没能成功登陆微博,革命尚未成功,同志仍需努力!

(以上代码运行时间2015/8/7,以后人人改了页面,也许也就不能登录了~~~)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值