과도한 매크로 사용은 서버에 큰 부담이 될 수 있으므로 반복적인 작업을 자동화 하거나 테스트 용도로 한정해서 사용하시기 바랍니다. 매크로 사용 시 발생하는 모든 문제에 대한 책임은 전적으로 매크로 사용자 본인에게 있습니다.
본 포스팅은 PS4 자동 구입 매크로라고 이름 지었지만 실제로는 python + selenium 강좌입니다. 예제를 쇼핑몰에서 물건 구입하는 것으로 들 뿐 대부분은 python + selenium 사용법에 치중할 것입니다. 또한 내용이 IT 비전공자에겐 어려울 수 있으며 기본적인 파이썬 사용법은 알고 있어야 합니다. 파이썬을 전혀 모르신다면 “파이썬 강좌”를 구글링하시면 좋은 강좌를 찾을 수 있습니다. 또한 셀레늄은 파이썬 뿐 아니라 java, c#, php, javascript 등 다양한 언어로 사용 가능하므로 본인에게 편한 것으로 고르면 되지만 여기서는 파이썬만을 다룹니다.
우분투, 맥, 윈도우 등 모두 가능하지만 여기선 Windows 10 PowerShell 환경만을 설명합니다.
우선 파이썬을 설치합니다. 아래 웹사이트에 가서 Windows X86 MSI Installer 를 받아 설치하면 됩니다.
https://www.python.org/downloads/release/python-2713/
이제 파워쉘을 실행해 봅니다. 그냥 명령 프롬프트(cmd.exe)를 사용해도 상관은 없습니다만 우리는 파워유저니까 파워쉘을 씁시다.
셀레늄 설치는 파워쉘에서 진행합니다. “pip install selenium” 을 입력하면 아래처럼 설치가 진행됩니다.
작업폴더를 만듭니다. 내문서 안에 macro 라고 폴더를 만들어도 되고 그냥 C:\ 에 다 때려 박아도 됩니다. 본인 취향대로 하세요. 전 기존에 쓰고 있던 git 저장소 밑에 “macro for ps4 pro” 라는 뜻으로 macpro 라고 만들었습니다.
이제 게코드라이버를 받아야 되는데 아래 사이트로 가서 64bit 윈도우면 geckodriver-v0.13.0-win64.zip, 32bit 윈도우를 쓰고 있거나 뭔지 모르면 그냥 geckodriver-v0.13.0-win32.zip를 받습니다.
https://github.com/mozilla/geckodriver/releases
압축을 풀어보면 geckodriver.exe 파일이 하나 있는데 이걸 아까 만든 작업폴더로 복사합니다.
마지막으로 웹브라우저를 설치합니다. 셀레늄은 chrome, firefox, IE 다 지원하지만 여기선 제일 잘 되는 파이어폭스를 사용합니다. 파이어폭스 설치는 아래 웹사이트에서 가능합니다.
https://www.mozilla.org/ko/firefox/new/?scene=2
설치가 다 잘되었는지 테스트를 해 보겠습니다. 우선 앞으로 쭉 사용할 파이썬 스크립트 파일을 만듭니다. 탐색기에서 새로만들기 하거나 파워쉘에서 “New-Item -ItemType file game.py” 라고 입력하면 됩니다.
이제 game.py 를 에디터로 열어서 아래처럼 코드를 입력합니다.
#-*- 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') if __name__ == "__main__": driver = webdriver.Firefox() driver.wait = WebDriverWait(driver, 2) driver.get("http://google.com")
if __name__ 아래 줄 인덴트도 정확히 입력하세요. 파이썬은 인덴트도 문법이기 때문에 인덴트 안 맞으면 에러 납니다. 인덴트로 탭을 쓰든 스페이스를 쓰든 상관없지만 전 스페이스 4칸을 인덴트 하나로 사용합니다.
저장 후 아래처럼 실행합니다. 파이썬은 이처럼 python <script name> 으로 실행됩니다.
실행했을 때 파이어폭스가 떠 주면 성공입니다.
이제 준비는 다 됐고 다음 포스팅 부터는 실제 웹페이지를 분석해 어떻게 자동화를 하는지 살펴보겠습니다. 여기까지 진행 중 잘 안되는 부분은 댓글로 달아주세요.
어떤 문제가 있을까요?
Traceback (most recent call last):
File "game.py", line 9, in <module>
reload(sys)
NameError: name 'reload' is not defined
이렇게 나오는데 어떻게하나요?
reload(sys)
sys.setdefaultencoding('utf-8')
를 다 지웠는데 작동이 됩니다. 그냥 둬도 되나요?
File "game.py", line 9, in <module>
reload(sys)
NameError: name 'reload' is not defined
윗분과 같은 문제를 겪고 있습니다.. 어떻게 해결해야 할까요
reload(sys)
sys.setdefaultencoding('utf-8')
이 세 줄을 그냥 지우고 해 보세요.
File "game.py", line 13, in <module>
driver = webdriver.Firefox()
File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 174, in __init__
keep_alive=True)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
파이어폭스가 깔려있는데 찾지 못하는것 같습니다.. 어떻게 해야 하나요?
https://stackoverflow.com/questions/46829641/webdriverexception-message-expected-browser-binary-location-but-unable-to-fin
pip install selenium 치면 pip 용어가 cmdlet 함수 스크립트 파일 또는 실행할수 있느 프로그램 이름으로 인식되지 않습니다 뜨네요 확인하고 경로가 올바른지 검증한다음 다시 시도하래요 ㅜㅜ
File "C:\Users\2010a\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\2010a\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\2010a\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 지정된 파일을 찾을 수 없습니다
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\2010a\game.py", line 9, in <module>
driver = webdriver.Firefox()
File "C:\Users\2010a\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 173, in __init__
self.service.start()
File "C:\Users\2010a\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 이렇게 떠요