├── README.md └── v2ex.js /README.md: -------------------------------------------------------------------------------- 1 | Javascript for robot 2 | ========= 3 | 4 | #### v2ex.com 5 | 6 | * 使用casperjs写的机器人在CLI下模拟浏览器实现 账号登陆,每日登录奖励,发表时间轴日志 7 | * 由于被强,目前https可以访问,CLI下需要添加 --ignore-ssl-errors=yes --ssl-protocol=tlsv1 参数 8 | * 参数 user,passwd 分别对应该站的账号和密码,show 打开调试信息 9 | * win下casperjs安装目录下casperjs.bat文件有一行修改为call phantomjs --ignore-ssl-errors=yes --ssl-protocol=tlsv1 "%CASPER_BIN%bootstrap.js" --casper-path="%CASPER_PATH%" --cli %ARGV% 10 | 11 | ```bash 12 | casperjs --ignore-ssl-errors=yes --ssl-protocol=tlsv1 --user=user --passwd=passwd --show=debug v2ex.js 13 | ``` 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /v2ex.js: -------------------------------------------------------------------------------- 1 | // v2ex login js 2 | var casper = require('casper').create({ 3 | verbose: true, 4 | logLevel: "debug" 5 | }); 6 | var user = casper.cli.get("user"); 7 | var passwd = casper.cli.get("passwd"); 8 | var debug = casper.cli.get('show'); 9 | debug = debug ? debug : 'info'; // debug, info, warning, error 10 | var once = 0; 11 | var url_mission = '1'; 12 | 13 | casper = require('casper').create({ 14 | verbose: true, 15 | logLevel: debug, 16 | }); 17 | 18 | casper.userAgent('Mozilla/5.0 (Linux; U; Android 2.2; en-us; ADR6300 Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'); 19 | 20 | casper.start('https://www.v2ex.com/signin', function() { 21 | this.wait(5000,function() { 22 | this.echo("I've waited for 5 seconds"); 23 | }); 24 | }).then(function(){ 25 | url = this.getCurrentUrl(); 26 | this.echo(url); 27 | //this.echo(this.getHTML()); 28 | this.capture("login.png"); 29 | }); 30 | 31 | // login form 32 | casper.waitForSelector('div.cell', function() { 33 | this.captureSelector('form.png', 'form[action="/signin"]'); 34 | once = this.getElementAttribute('input[name="once"]', 'value'); 35 | str = this.getElementsAttribute('input.sl', 'name'); 36 | url_mission = 'https://v2ex.com/mission/daily'; 37 | //this.fillSelectors('form[action="/signin"]', { 38 | // name : '', 39 | // passwd : '', 40 | // 'input[name="next"]' : '/', 41 | // 'input[name="once"]' : this.getElementAttribute('input[name="once"]', 'value') 42 | //}, false); 43 | var f_name = str[0]; 44 | var f_passwd = str[1]; 45 | var data = { 46 | 'next' : '/', 47 | 'once' : this.getElementAttribute('input[name="once"]', 'value') 48 | }; 49 | data[f_name] = user; 50 | data[f_passwd] = passwd; 51 | //require('utils').dump(data); 52 | this.fill('form[action="/signin"]', data, false); 53 | //this.click('input[value="登录"]'); 54 | //this.echo(this.getCurrentUrl()); 55 | this.capture("login-fill.png"); 56 | //this.echo('login clicked...'); 57 | }); 58 | 59 | casper.thenClick('input[value="登录"]', function(){ 60 | this.echo(this.getCurrentUrl()); 61 | this.echo(once); 62 | this.capture("logined.png"); 63 | }) 64 | 65 | // todo 此处好像有点问题,没有领成功 66 | casper.then(function(){ 67 | this.open(url_mission); 68 | }).then(function(){ 69 | this.capture("daily.png"); 70 | }).thenClick('input[value="领取 X 铜币"]', function(){ 71 | this.capture("daily-do.png"); 72 | }) 73 | 74 | 75 | casper.then(function(){ 76 | this.open("https://v2ex.com/t"); 77 | }) 78 | 79 | casper.waitForSelector('div.box', function() { 80 | var time = new Date().toLocaleString(); 81 | this.sendKeys('div.box textarea#s', "我发了一条日志,使用机器人 casperjs!!" + time); 82 | this.capture("send.png"); 83 | }); 84 | 85 | casper.thenClick('input[value="发布"]', function(){ 86 | this.echo('send...'); 87 | this.wait(2000,function() { 88 | this.echo("I've waited for 2 seconds"); 89 | }); 90 | }).then(function(){ 91 | this.open("https://v2ex.com/t"); 92 | }).then(function(){ 93 | this.capture("msg.png"); 94 | }); 95 | 96 | casper.run(); 97 | //测试一下,谢谢 98 | //console.log(url_mission); 99 | //console.log(once); 100 | 101 | 102 | 103 | 104 | 105 | 106 | --------------------------------------------------------------------------------