Created
February 5, 2017 13:35
-
-
Save teeceepee/9ea9066bda7dbf975b3b9711ebecd938 to your computer and use it in GitHub Desktop.
Weibo login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: phantomjs weibo-login.js [username] [password] | |
// API: http://phantomjs.org/api/ | |
var system = require('system'); | |
// Arguments | |
var username = system.args[1] | |
var password = system.args[2] | |
if (!username || !password) { | |
console.log("Usage: phantomjs " + system.args[0] + " [username] [password]") | |
phantom.exit() | |
} | |
// Constants | |
var LOGIN_URL = "http://weibo.com/login.php" | |
var HOME_URL_PATTERN = new RegExp("http://weibo.com/.*/home") | |
// Utils | |
var getCookies = function (page) { | |
var cookieString = page.cookies.map(function (cookie) { | |
return cookie.name + "=" + cookie.value | |
}).join("; ") | |
return "Cookie: " + cookieString | |
} | |
// Login simulation | |
var page = require('webpage').create() | |
page.onUrlChanged = function (targetUrl) { | |
if (targetUrl !== "about:blank") { | |
console.debug("Loading url: " + targetUrl) | |
} | |
// 如果跳转到用户主页则表示登录成功 | |
if (HOME_URL_PATTERN.test(targetUrl)) { | |
var cookies = getCookies(page) | |
console.log("\n") | |
console.log(cookies) | |
console.log("\n") | |
setTimeout(function () { | |
phantom.exit() | |
}, 1) | |
} | |
} | |
page.open(LOGIN_URL, function (status) { | |
if (status !== "success") { | |
console.debug("Failed to load url " + LOGIN_URL) | |
phantom.exit() | |
} | |
// 填写用户名密码并点击提交按钮 | |
page.evaluate(function (username, password) { | |
var usernameInput = document.querySelector("[name=username]") | |
var passwordInput = document.querySelector("[name=password]") | |
var submitBtn = document.querySelector('[node-type="submitBtn"]') | |
usernameInput.value = username | |
passwordInput.value = password | |
submitBtn.click() | |
}, username, password) | |
setTimeout(function () { | |
console.debug("Login Failed") | |
phantom.exit() | |
}, 10000) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment