简体   繁体   English

PhantomJS登录和页面重定向

[英]PhantomJS Login and Page Redirect

I am trying to crawl a website which kind of have a login page seperated and jumping homepage after login. 我正在尝试爬网一个具有单独登录页面的网站,并在登录后跳转主页。 Here is my code, but I am not accomplishment jumping homepage: 这是我的代码,但是我没有成就跳转主页:

var page = require('webpage').create() ;
var login = 'https://webstie.com/login' ;
var home = 'https://website.com/home' ;

page.open(login, function (status) {
    if (status !== 'success') {
        console.log('fail!');
    } else {
        page.evaluate(function(){
            function timer (f,n) {
                var i = 0 ;
                var t = setInterval(function(){
                    if (n < i) {
                        clearInterval(t) ;
                        f() ;
                    }
                    i++ ;
                },50) ;
            }
            $("input[name=email]").val("user") ;
            $("input[name=password]").val("pass") ;
            $("input[type=submit]").click() ;
            timer(function(){
                document.location.href = home ;
                timer(function(){
                    $('body').css('border','1px solid red') ;
                },100) ;
            },100) ;
        }) ;
        page.render('page.png') ;
    }
    console.log('finished!') ;
    phantom.exit() ;
});

You forgot to wait for asynchronous processing. 您忘记等待异步处理。 Your timer() function is asynchronous, because setTimeout() is asynchronous. 您的timer()函数是异步的,因为setTimeout()是异步的。 That is why your page.render() call happens actually before the timer() has run. 这就是您的page.render()调用实际上在timer()运行之前发生的原因。 The same goes for phantom.exit() . phantom.exit()

But you don't want to use document.location.href = home , because then you need to listen to the page open event. 但是,您不想使用document.location.href = home ,因为那样您就需要监听页面打开事件。 You can do this in an integrated fashion with another page.open() . 您可以与另一个page.open()以集成方式进行此操作。

Try: 尝试:

page.open(login, function (status) {
    if (status !== 'success') {
        console.log('fail!');
        phantom.exit(1);
    } else {
        page.evaluate(function(){
            $("input[name=email]").val("user") ;
            $("input[name=password]").val("pass") ;
            $("input[type=submit]").click() ;
        });
        setTimeout(function(){
            page.open(home, function(status){
                if (status !== "success") {
                    console.log('fail2');
                    phantom.exit(1);
                    return;
                }
                page.evaluate(function(){
                    $('body').css('border','1px solid red') ;
                });
                page.render('page.png');
                console.log('finished!');
                phantom.exit();
            });
        }, 500);
    }
});

Use waitFor() for more robust waiting for a specific condition or use the page.onCallback and window.callPhantom() pair . 使用waitFor()获得更强大的等待特定条件的性能,或使用page.onCallbackwindow.callPhantom()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM