gencode
23479 4
PS4 프로 자동 구입 매크로 만들기 -2-
2017-01-12 19:36:27 - genonfire
2019-09-23 11:23:33
과도한 매크로 사용은 서버에 큰 부담이 될 수 있으므로 반복적인 작업을 자동화 하거나 테스트 용도로 한정해서 사용하시기 바랍니다. 매크로 사용 시 발생하는 모든 문제에 대한 책임은 전적으로 매크로 사용자 본인에게 있습니다.


지난 포스팅에서 브라우저를 띄우는 데까지 성공 했습니다.


if __name__ == "__main__":
    driver = webdriver.Firefox()
    driver.wait = WebDriverWait(driver, 2)

    driver.get("http://google.com")


이처럼 driver가 셀레늄의 webdriver 인스턴스이고 실제 셀레늄에서 제공하는 많은 기능들을 다 이 driver를 통해서 사용합니다. 더 많은 기능을 알고 싶거나 예습이 필요하면 아래 사이트를 방문해 보세요. 참고로 selenium도 그렇고 이 업계에는 한글로 된 문서가 거의 없습니다.

http://selenium-python.readthedocs.io/index.html 

https://www.guru99.com/selenium-tutorial.html


다시 위 소스로 돌아가서 첫 두 줄은 뭔가 초기화 하는 부분이고 driver.get()은 특정 사이트로 접속하는 건데 이런 사소한 건 그냥 그런갑다 하고 복사 붙여넣기 하시면 됩니다. 우리에게 중요한 건 어떻게 사이트를 분석해서 자동으로 입력을 처리하느냐이지 책 보듯 소스를 공부하는 건 필요 없으니까요. 따라서 앞으로도 소스 한 줄 한 줄은 따로 설명하지 않습니다.  이런 기능을은 이렇게 구현한다는 걸 예시를 보여드리니 복사 붙여넣기로 쓰시거나 본인의 상황에 맡게 살짝 수정 및 응용해서 사용하시면 됩니다.


시나리오 만들기

우선 디자인을 해 봅시다. 직접 쇼핑몰에 접속 해 물건 하나 구입할 때 필요한 작업들을 단계별로 진행하면서 과정을 적어보세요. 그리고 각 과정에서 내 손으로 입력해야 되는 부분도 같이 체크해 봅니다. 이 부분이 실제로 코딩을 해야 되는 부분입니다.


1. 홈페이지 접속

2. 로그인 링크 누름

3. 아이디/패스워드 입력 후 로그인 버튼 누름

4. 구입하려는 물품 페이지로 이동

5. 재고여부 확인

    5-1. 품절이면 새로고침

    5-2. 구입 가능하면 바로 구매하기 버튼 누름

6. 주문서 작성 후 결제하기버튼 누름


콘솔게임 국내판매 1위 쇼핑몰에서 해보니 위와 같은 시나리오가 나왔습니다. 미리 말씀드립니다만 이 과정에서 주문서 완료하는 데 까지만 예제를 드리고 시간이 되면 다른 쇼핑몰 하나 더 선정해서 예제를 만들어 보겠습니다. 이후 과정 및 원하는 시나리오 적용 등은 스스로 응용해 보세요.


다시 시나리오에서 사실 1과 2는 하나로 합칠 수 있습니다. 바로 로그인 링크로 접속하면 되죠. 이 시나리오를 코드로 표현해 보면 아래와 같습니다.


#-*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

WAITTIME = 1000
SLEEPSEC = 5
LOOP = 3 # -1 for infinite

HOMEPAGE = "http://gamewoori.com/shop/member.html?type=login"
ITEMURL = "http://gamewoori.com/shop/shopdetail.html?branduid=885806&xcode=009&mcode=001&scode=&type=X&search=&sort=regdate"

def login(driver):
    print 'login 처리 함수'

def checkStock(driver, i):
    print '재고 확인 함수'

def checkout(driver):
    print '구매하기 누르는 함수'

def order(driver):
    print '주문서 작성 함수'

if __name__ == "__main__":
    driver = webdriver.Firefox()
    driver.wait = WebDriverWait(driver, 2)

    driver.get(HOMEPAGE)

    login(driver)
    driver.get(ITEMURL)

    i = 1
    while(checkStock(driver, i)):
        i += 1
        time.sleep(SLEEPSEC)
        if (i > LOOP and LOOP != -1):
            break
        driver.get(ITEMURL)

    checkout(driver)

    order(driver)


쉽죠? 이제 우리는 맨 위에 있는 login 부터 order 까지 함수 4개만 작성하면 됩니다. 다음 포스팅에서는 자동 로그인을 만들어 보도록 하겠습니다.

이 글이 마음에 드셨다면.. 4
gencode 의 다른 포스트
PS4 프로 자동 구입 매크로 만들기 -5-
PS4 프로 자동 구입 매크로 만들기 -4-
PS4 프로 자동 구입 매크로 만들기 -3-
> PS4 프로 자동 구입 매크로 만들기 -2-
PS4 프로 자동 구입 매크로 만들기 -1-
블루호스트 사용기
댓글 [ 4 ]
Guest 09-21
Hi,

I enjoyed your article on https://gencode.me/blogs/post/44/. I especially liked how lucid the article was.

I gather you have linked to http://selenium-python.readthedocs.io/ from that page.

Would you consider linking to us?

Here is our link - Selenium

I did be happy to share your page with our 30k Facebook/Twitter/Linkedin Followers.

Regards,
Alex
Hi, I enjoyed your art 09-21
Hi,

I enjoyed your article on https://gencode.me/blogs/post/44/. I especially liked how lucid the article was.

I gather you have linked to http://selenium-python.readthedocs.io/ from that page.

Would you consider linking to us?

Here is our link - Selenium

I did be happy to share your page with our 30k Facebook/Twitter/Linkedin Followers.

Regards,
Alex
Guest 09-19
Hi,

I'll keep this short and sweet to make the 34 seconds it takes to read this worth your time (yes, I timed it.)

I saw you recently linked to http://selenium-python.readthedocs.io/ from https://gencode.me/blogs/post/44/.

I hope you could add my link https://www.guru99.com/selenium-tutorial.html

Of course, I don’t want a free from you. I wouldn't reply to this kind of email if there wasn’t anything for me, so here’s what you can get:

• Social sharing of your article
• You will update the content and we all know Google likes that
• You will make me extremely happy

That's it. What do you think?

Best,
[Alex]
genonfire 09-23
I added a link https://www.guru99.com/selenium-tutorial.html because it seems to useful to my readers.