使用casperjs抓取http://bm.scs.gov.cn/2015/StudentLogin.aspx

var casper = require('casper').create({   
    verbose: true, 
    logLevel: 'debug',
    pageSettings: {
         userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:37.0) Gecko/20100101 Firefox/37.0',
         loadImages: true,
         loadPlugins: true
    }
});
//文件处理
var fs = require('fs');

casper.options.viewportSize = {width: 1680, height: 924};
casper.start('http://bm.scs.gov.cn/2015/StudentLogin.aspx');
casper.then(function(){
    this.echo('开始获取cookie中的验证码');
    var phantom_cookie = phantom.cookies;
    var cookie_info = '';
    for (var i in phantom_cookie){
        if(phantom_cookie[i]['name']=='ValicodeCookie'){
             cookie_info = phantom_cookie[i]['value'];
        }
    }
    if(cookie_info !==''){
        cookie_array = cookie_info.split('=');
        input = cookie_array[1];
    }else{
        input = '';
    }
    this.echo('获取cookie中的验证码是'+input);
    this.sendKeys('form#Form1 input#Loginbystudent1_txt_vcode',input);
});

//casper.wait(5*1000,function init(){
//    this.echo('开始截图');
//    this.captureSelector('lo.jpeg','#Loginbystudent1_Image1');
//    this.echo('截图完成');
//});
//
//casper.then(function(){
//    this.echo('请输入验证码:');
//    var input = system.stdin.readLine();
//    this.echo('你输入的是:'+(input));
//    this.sendKeys('form#Form1 input#Loginbystudent1_txt_vcode',input);
//});
casper.waitForSelector("form#Form1",
    function success() {
        this.test.assertExists("form[id='Form1']");
        this.fill('form[id="Form1"]',{
            'Loginbystudent1$txt_Identify':'username',
            'Loginbystudent1$txtPassword':'password'
        },false);
        this.click("a#Loginbystudent1_btn_Login");
    },
    function fail() {
        this.test.assertExists("form[id='Form1']");
});

casper.waitFor(function check() {
    return this.getCurrentUrl().indexOf("http://bm.scs.gov.cn/2015/UserControl/")>-1;
}, function then() {
    this.echo('登陆成功');
}).then(function(){
    //this.echo("打开链接并截图");
    this.echo('当前的页面链接 ' + this.getCurrentUrl());
    this.capture("./me.png");
});
//把cookie写入文件
casper.wait(1000, function () {
    this.echo('保存cookie文件');
    var cookies = JSON.stringify(phantom.cookies);
    cookie_file = 'login_cookie.txt';
    fs.write(cookie_file, cookies, 644);
    this.echo("cookie写入成功 : " + cookie_file +"\n");
});

casper.run(function() {this.test.renderResults(true);});
//casper.exit();

你可能感兴趣的:(php)