使用casperjs登录网站?

时间:2016-04-22 05:13:42

标签: node.js web-scraping phantomjs casperjs

我尝试使用casperjs登录网站。

我试试这段代码。

var casper = require('casper').create({
    pageSettings: {
        loadImages: false,
        loadPlugins: false,
        userAgent: 'Mozilla/45.0.2 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36'
    }
});

casper.start().thenOpen("https://sellercentral.amazon.in/gp/homepage.html", function() {
    console.log("amazon site  website opened");
    var html = this.getPageContent();
    console.log(html);
});
casper.then(function(){
    console.log("Login using username and password");
    this.evaluate(function(){
        document.getElementById("username").value="******";
        document.getElementById("password").value="******";
        document.getElementById("sign-in-button").click();
    });
});
casper.then(function(){
console.log("Make a screenshot and save it as AfterLogin.png");
    this.wait(40000);
    this.capture('AfterLogin.png');
});
casper.run();

但是我得到了输出

 <html><head></head><body></body></html>  
   Login using username and password
   Make a screenshot and save it as AfterLogin.png
   []

当我打印仅在html标签上方打印的console.log(html)时。 我也尝试相同的代码为facebook.com仍然相同。

2 个答案:

答案 0 :(得分:0)

您可以使用nightmare.js或phantom.js。两者都允许我从网站上读取html并模拟事件。

或者,您可以检查端点,只需点击它们即可检索数据

答案 1 :(得分:0)

您的问题可能会绑定到CAPTCHA测试通过失败(在loadImages: false XPath上使用//*[@id="ap_captcha_img"]/img时,您会在给定的响应中错过它。

在CasperJS / 1.1.1 + PhantomJS / 2.1.1设置中,使用类似的代码

var sURL = 'https://sellercentral.amazon.in/gp/homepage.html';
var casper = require("casper").create({
    pageSettings: {
        loadImages: true,
        userAgent: 'Mozilla/45.0.2 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36'
    }
});

casper.start();
casper.thenOpen(sURL, function() {
    console.log("website opened");
    var html = this.getPageContent();
    console.log(html);
});
casper.then(function(){
    console.log("Login using username and password");
    this.fill('form[name=signinWidget]', {
            'username': 'username',
            'password': '123456'
        }, true);
});
casper.wait(10000, function() {
    console.log("Make a screenshot and save it as AfterLogin.png");
    this.capture('AfterLogin.png');
});

casper.run(function() {
    this.exit();
});

得到以下输出

website opened
<!DOCTYPE html><html><head>

<!--
whole resource HTML
-->

</body></html>
Login using username and password
Make a screenshot and save it as AfterLogin.png

enter image description here