레이블이 casperjs인 게시물을 표시합니다. 모든 게시물 표시
레이블이 casperjs인 게시물을 표시합니다. 모든 게시물 표시

2017년 11월 3일 금요일

casperjs 팁

casperjs 로 대여섯개 사이트 자동 출첵을 걸어놨다.

삽질 중에 얻은 팁을 공유한다.

 

1. <input> 에 type 이 없을 경우 fill 이 안 먹힘. << 이런 개 같은 경우가 있음  그럴 때는 sendKey를 활용.

2. alert 한글이 깨지는 경우 setting 옵션에 encoding: "utf8", 를 넣으면 됨.

3. verbose: true로 해야 syntax 에러 나옴.

4.onsubmit="INVEN.Outlogin.login(this); return false;"   << 이런식으로 되어 있으면 fill true 가 제대로 동작 안함. click 이벤트로 처리 하도록

(INVEN이네..;;)

 

11번가 처리시
5. iframe 처리iframe접근은 casper.withFrame 를 사용.

 
==================[debug] [phantom] opening url: http://www.11st.co.kr/browsing/NewPlusZonePlace.tmall?method=getEventPage&addCtgrNo=951965, HTTP GET[debug] [phantom] Navigation requested: url=http://www.11st.co.kr/browsing/NewPlusZonePlace.tmall?method=getEventPage&addCtgrNo=951965, type=Other, willNavigate=true, isMainFrame=true  << isMainFrame이 true[debug] [phantom] url changed to "http://www.11st.co.kr/browsing/NewPlusZonePlace.tmall?method=getEventPage&addCtgrNo=951965"[debug] [phantom] Navigation requested: url=http://www.11st.co.kr/html/blank.html, type=Other, willNavigate=true, isMainFrame=false															<< isMainFrame이 false임. iframe이란 소리.[debug] [phantom] Navigation requested: url=http://www.11st.co.kr/jsp/event15/150625_benefitZoneAttend/iframe.jsp, type=Other, willNavigate=true, isMainFrame=false							<< iframe[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false																					<< iframe[debug] [phantom] Navigation requested: url=https://vars.hotjar.com/rcj-99d43ead6bdf30da8ed5ffcb4f17100c.html, type=Other, willNavigate=true, isMainFrame=false								<< iframe[debug] [phantom] Successfully injected Casper client-side utilities[info] [phantom] Step anonymous 5/8 http://www.11st.co.kr/browsing/NewPlusZonePlace.tmall?method=getEventPage&addCtgrNo=951965 (HTTP 200)[info] [phantom] Step anonymous 5/8: done in 6937ms.[info] [phantom] Step _step 6/8 http://www.11st.co.kr/browsing/NewPlusZonePlace.tmall?method=getEventPage&addCtgrNo=951965 (HTTP 200)[info] [phantom] Step _step 6/8: done in 6955ms.==================

위의 로그를 보면 iframe이 4개임.
iframe 이름이 있으면 casper.withFrame('이름'... 이런식으로 주지만, 이름이 없으면 0부터 index를 준다.

11번가 자동 출첵, 2017.11.03 (casperjs)

기존에 curl 을 이용한 방식은 onclick등 자바스크립트 함수가 들어가면 매우 골치아파지는 문제가 있었다.

그래서 검색하다 'headless browser' 라는 걸 알게 됐고, casperjs 란 알게 됐다.


 

11번가 자동 출첵은 casperjs 로 만들어서 기존에 사용하고 있었는데,

9월 30일 이후로 출첵이 안된 거 보니 그 때 출석체크 페이지가 바뀐듯 하다.

 

11번가 자동 출첵은 왜 하느냐?

마일리지를 받기 위해서다. 마일리지로 뭘 할 수 있는냐?

사람들이 잘 모르는데, 11번가 마일리지로 할 수 있는게 많다.

상품쿠폰,배송비쿠폰, 핸드폰 데이터 쿠폰 그리고 상품 교환도 된다.

베스킨라빈스 레귤러 한번 먹어 봤다.

 

각설하고 11번가 자동 출체 코드는 아래와 같다.
var casper = require('casper').create({
    pageSettings: {
        "userAgent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
        "loadImages": true,
        "loadPlugins": false,
        "webSecurityEnabled": false,
        "ignoreSslErrors": true
    },
    onWaitTimeout: function () {
        //throw new Error
    },
    onStepTimeout: function () {
        //throw new Error
    },
    encoding: "utf8",
    waitTimeout: 10000,
    stepTimeout: 10000,
     logLevel: "debug",              // Only "info" level messages will be logged
     verbose: true                  // log messages will be printed out to the console
});

var login_id = casper.cli.get("id");
var login_pw = casper.cli.get("pw");
var login_url = 'https://login.11st.co.kr/login/Login.tmall';
var attendance_url = 'http://www.11st.co.kr/browsing/MallPlanDetail.tmall?method=getMallPlanDetail&planDisplayNumber=935566';

//temp
login_id = 'myid';
login_pw = 'mypass';

if(!login_id){
    casper.echo("require id parameter");
//    casper.exit();  //not working
    phantom.exit();
}
if(!login_pw){
    casper.echo("require pw parameter");
    phantom.exit(1);
}


casper.start(login_url, function() {
    this.fill('form[name="login_form"]', {
        'loginName' : login_id,
        'passWord': login_pw
    }, false);

    this.click('#memLogin > div.save_idW > input');
    this.wait(1000, function() {
        //this.echo("I've waited for a second.");
    });
});


//출석
casper.thenOpen(attendance_url, function(){
    //iframe
    this.withFrame(1, function () {
		this.click('#regForm > div > div.sect03 > div.dev04 > a.get04 > img');
		this.wait(1000, function() {
			this.setFilter("page.confirm", function(msg) {
				return true;
			});
		});
	});

});

//casper.run();
casper.run(function() {
    require('utils').dump(this.result.log);
    this.exit();
});

 

당연하지만 매일 자동으로 실행하려면 매일 켜져있는 pc(server)가 필요하다.

테스트용 리눅스 pc가 있어서 casperjs 설치 후 cron으로 매일 실행 되게끔 설정하였다.

 

없으신 분들은 알아서...;;