PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.
开搞,代码实现如下。
var page = require('webpage').create();
var fs = require("fs");
page.settings.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4';
page.open('http://ui.ptlogin2.qq.com/cgi-bin/login?pt_no_onekey=1&style=9&appid=1006102&s_url=http%3A%2F%2Fxiaoqu.qq.com%2Fmobile%2Fbarindex.html%3F_lv%3D29313%26_bid%3D128%26_wv%3D1027%26from%3Dshare_link%23bid%3D37469%26type%3D%26source%3Dindex%26scene%3Drecent%26from%3Ddongtai%26webview%3D1&low_login=0&hln_css=http%3A%2F%2Fpub.idqqimg.com%2Fqqun%2Fxiaoqu%2Fmobile%2Fimg%2Fnopack%2Flogin-logo.png', function(status){
if (status == 'success') {
page.render('index.png');
setTimeout(function() {
page.evaluate(function() {
document.getElementById('u').value = 'QQ号码';
document.getElementById('p').value = 'QQ密码';
pt.check(false);
//document.getElementById('go').click(); //pt.check()或.click()
});
setTimeout(function() {
file = fs.open("cookie.log", 'w');
file.write(JSON.stringify(page.cookies));
file.close();
phantom.exit();
}, 2000);
}, 1000);
}
});
cookie会写入到当前目录下的cookie.log文件,有了cookie接下来的事情就简单多了。使用php或js将cookie种到浏览器下即可。
优化后的qq.js代码:
/**
* QQ自动登录,用phantomjs模拟浏览器,自动登录到QQ兴趣部落
* @example 执行方式:cd /phantom && ./phantomjs qq.js QQ号 QQ密码
* phantomjs下载:http://phantomjs.org/download.html
* 安装依赖(重要,否则会报错):
* sudo yum install fontconfig freetype libfreetype.so.6 libfontconfig.so.1 libstdc++.so.6
*/
var page = require('webpage').create();
var fs = require("fs");
var args = require('system').args;
page.settings.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4';
page.open('http://ui.ptlogin2.qq.com/cgi-bin/login?pt_no_onekey=1&style=9&appid=1006102&s_url=http%3A%2F%2Fxiaoqu.qq.com%2Fmobile%2Fbarindex.html%3F_lv%3D29313%26_bid%3D128%26_wv%3D1027%26from%3Dshare_link%23bid%3D37469%26type%3D%26source%3Dindex%26scene%3Drecent%26from%3Ddongtai%26webview%3D1&low_login=0&hln_css=http%3A%2F%2Fpub.idqqimg.com%2Fqqun%2Fxiaoqu%2Fmobile%2Fimg%2Fnopack%2Flogin-logo.png', function(status){
if (status == 'success') {
page.render('index.png');
setTimeout(function() {
page.evaluate(function(_args) {
document.getElementById('u').value = _args[1]; //QQ号码
document.getElementById('p').value = _args[2]; //QQ密码
pt.check(false);
}, args); //要使用传参的形式将全局变量args传入到page.evaluate()
setTimeout(function() {
//将cookie转换成json格式写入到cookie.log文件
//file = fs.open("cookie.log", 'w');
//file.write(JSON.stringify(page.cookies));
//file.close();
//将cookie转换成json格式打印出来
console.log(JSON.stringify(page.cookies));
phantom.exit(); //记得退出
}, 2000);
}, 1000);
}
});
qq.js也可以使用以下代码进行自动登录:
page.open('https://ui.ptlogin2.qq.com/cgi-bin/login?style=9&appid=1006102&daid=0&s_url=http%3A%2F%2Fid.qq.com&low_login=0', function(status){
if (status == 'success') {
//page.render('index1.png'); //截屏,为了调试使用
setTimeout(function() {
page.evaluate(function(_args) {
document.getElementById('u').value = _args[1]; //QQ号码
document.getElementById('p').value = _args[2]; //QQ密码
document.getElementById('go').click(); //pt.check()或.click()
}, args); //要使用传参的形式将全局变量args传入到page.evaluate()
setTimeout(function() {
//将cookie转换成json格式写入到cookie.log文件
//file = fs.open("cookie.log", 'w');
//file.write(JSON.stringify(page.cookies));
//file.close();
//将cookie转换成json格式打印出来
console.log(JSON.stringify(page.cookies));
//page.render('index.png'); //截屏,为了调试使用
//console.log(page.content); phantom.exit(); //记得退出 }, 2000); }, 1000); } });
php调用phantomjs代码:
public function actionQqLogin() {
ret = false;qq = 'QQ号';
pwd = 'QQ密码';command = "cd /phantom/ && ./phantomjs qq.js {qq} {pwd}";
cookie_json = @exec(command); //只返回最后一行
//echo $cookie_json.'
'; //test
if (cookie_json) {cookie_arr = json_decode(cookie_json, true);
//print_r(cookie_arr); //test
if (cookie_arr) {
foreach (cookie_arr as cookie) {
//echocookie['value'].' ';
//注意:不要用setcookie(),用setrawcookie()不会对cookie value进行url编码
ret = setrawcookie(cookie['name'], cookie['value'],cookie['expires'], '/', cookie['domain']);
}
}
}
if (ret) {
echo '登录成功!';
}
//return $ret;
}
微信扫一扫,打赏作者吧~