Phantom.js登录Instagram页面 [英] Phantom.js login instagram page

查看:94
本文介绍了Phantom.js登录Instagram页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用phantom.js登录instagram网站.我的第一种方法是:

I am trying use phantom.js to login in instagram website. My first approach was:

document.querySelector("input[name='username']").value = "Username";
document.querySelector("input[name='password']").value = "Pass";

但是此代码不会更改da DOM.

But this code don't change da DOM.

第二种方法:

document.getElementsByClassName("_kp5f7 _qy55y")[0].setAttribute("value", "Username");
document.getElementsByClassName("_kp5f7 _qy55y")[0].value = "Pass";

但是当我检查网络软件包时,用户名和密码字段为空白.

But when I inspect the network packages fields username and pass are blank.

Instagram登录页面: https://www.instagram.com/accounts/login/

Instagram login page: https://www.instagram.com/accounts/login/

推荐答案

更新:请参见答案下方的修改

我真的不这么认为

PhantomJS无法处理该页面

PhantomJS can't handle that page

可能我们在模拟真实浏览器方面做得不够好.

It's probably us not doing good enough of impersonation of a real browser.

快速搜索"instagram登录phantomjs",发现此整洁的解决方案有效:

A quick search for "instagram login phantomjs" found this neat solution that works: https://github.com/awener/instagram-login-phantomjs/blob/master/phan.js

它使用PhantomJS机制模拟真实"的按键和点击.

It uses PhantomJS mechanism of simulating "real" keypresses and clicks.

这里是脚本的副本,以防万一.

Here's a copy of the script just in case.

var page = require('webpage').create();
var username = "myusername";
var password = "password";
page.viewportSize = { width: 1024 , height: 600 };
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36';

page.open('https:/instagram.com/accounts/login/', function() {

    var ig = page.evaluate(function() {
        function getCoords(box) {
            return  {
                x: box.left,
              y: box.top 
            };
        }   

        function getPosition(type, name) {
            // find fields to fill
            var input = document.getElementsByTagName(type);
            for(var i = 0; i < input.length; i++) {
                if(name && input[i].name == name)  return getCoords(input[i].getBoundingClientRect());
                else if(!name && input[i].className)    return getCoords(input[i].getBoundingClientRect()); // this is for login button
            }
        }
        return {
            user: getPosition('input', 'username'),
            pass: getPosition('input', 'password'),
            login: getPosition('button')
        };

     });

     // fill in data and press login
     page.sendEvent('click',ig.user.x, ig.user.y);
     page.sendEvent('keypress', username);

     page.sendEvent('click',ig.pass.x, ig.pass.y);
     page.sendEvent('keypress', password);
     page.sendEvent('click', ig.login.x, ig.login.y);

    // wait for response
    setTimeout(function() {
        page.render('/path/to/screenshot.png');
        phantom.exit();
    }, 5000);

});


编辑并说明如何在Linux上运行脚本

这在Debian/Ubuntu上不起作用的原因是SSL证书问题.

The reason this did not work on Debian/Ubuntu is SSL certificate issues.

当使用--debug = true CLI选项运行PhantomJS时,有一个详细的模式说明PhantomJS在做什么.使用它,我发现了问题的原因:

There is a verbose mode telling about what PhantomJS is doing, when you run it with --debug=true CLI option. Using that I've found the cause of the problem:

[DEBUG] Network - SSL Error: "The issuer certificate of a locally looked up certificate could not be found"
[DEBUG] Network - SSL Error: "The root CA certificate is not trusted for this purpose"
[DEBUG] Network - Resource request error: QNetworkReply::NetworkError(SslHandshakeFailedError) ( "SSL handshake failed" ) URL: "https://instagramstatic-a.akamaihd.net/h1/scripts/polyfills/es5-sham.min.js/fc3c22cf2d67.js"
...

要避免此类问题,您只需运行带有另一个CLI参数的Phantomjs,告诉它忽略SSL错误:

To avoid this type of problems you just have to run Phantomjs with another CLI argument telling it to ignore SSL errors:

/pth/to/phantomjs --ignore-ssl-errors=true /path/to/script.js

这篇关于Phantom.js登录Instagram页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆