한때 마스크 대란으로 인터넷을 통한 마스크 구매에 한창 열을 올리던 때가 있었다. 지금이야 공적 마스크의 보급으로 인해 절박하지는 않지만 그 때 당시 마스크 구매는 매우 어려웠다. 


울며 겨자 먹기로 천마스크를 여러 장 구매하거나 필터만 갈아 끼울 수 있는 제품도 구매하고.. 지금 생각해 보면 촌극이었다. 이러던 중 회사 동료가 착한 마스크라 하여 아에르(AER) 마스크 구매 SITE 및 요령을 알려주어 근 한달간 매일 아침 9시 45분만 되면 소리없는 전쟁이 벌어졌었다. 이 때 PYTHON을 활용하여 자동 구매 매크로 프로그램을 만들면 어떨까 생각하여 인터넷을 뒤져가며 조악스럽지만 동작은 가능한 프로그램을 만들어 보았다. 


결론은 한번도 성공하지 못했고, 수동으로 구매했던 회사 동료는 간간히 구매할 수 있었다. 단지 PYTHON 학습의 기회로만 활용한 시기였었다.


최근에 비말 차단용 마스크라 하여 웰킵스몰등에서 9시면 또 다시 총성없는 전쟁이 벌어지는 것 같다. 혹시라도 자동 구매 프로그램 제작 전문가가 이 글을 보시게 되면 도움 말씀 꼭 부탁드리고 싶다.



from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import pyperclip
import time
import sys
from PyQt5.QtQuickWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QThread, QObject
from PyQt5 import uic
import threading

UserUI = uic.loadUiType('autobuy.ui')[0]

class AutoBuy(QMainWindow, UserUI):
def __init__(self, *args, **kwargs):
super(AutoBuy, self).__init__(*args, **kwargs)
UserUI.__init__(self)
self.setupUi(self)
self.ID = '*******'
self.PASSWORD = '********'
self.HOMEPAGE = 'https://nid.naver.com/nidlogin.login?mode=form&url=https%3A%2F%2Fwww.naver.com'
self.ITEMURL = "https://smartstore.naver.com/aer-shop/products/4722827602"
self.lineEdit.setText(self.HOMEPAGE)
self.lineEdit_2.setText(self.ITEMURL)
self.lineEdit_3.setText(self.ID)
self.lineEdit_4.setText(self.PASSWORD)
self.pushButton.clicked.connect(self.Mythread)
self.pushButton_2.clicked.connect(self.End)
self.pushButton_3.clicked.connect(self.Login)
self.pushButton_4.clicked.connect(self.test)
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(1)

#클립보드에 input을 복사한 뒤
#해당 내용을 actionChain을 이용해 로그인 폼에 붙여넣기
def copy_input(self, xpath, input):
pyperclip.copy(input)
self.driver.find_element_by_xpath(xpath).click()
ActionChains(self.driver).key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
time.sleep(1)

def Login(self):
self.HOMEPAGE = self.lineEdit.text()
self.ITEMURL = self.lineEdit_2.text()
self.ID = self.lineEdit_3.text()
self.PASSWORD = self.lineEdit_4.text()
self.driver.get(self.HOMEPAGE)
self.copy_input('//*[@id="id"]', self.ID)
time.sleep(1)
self.copy_input('//*[@id="pw"]', self.PASSWORD)
time.sleep(1)
self.driver.find_element_by_xpath('//*[@id="frmNIDLogin"]/fieldset/input').click()
self.driver.find_element_by_xpath('//a[@id="new.save"]').click()
self.driver.get(self.ITEMURL)

def Buy(self):
try:
self.driver.get(self.ITEMURL)
self.driver.find_element_by_xpath('//div[@class="selectbox-label"]').click()
self.driver.find_element_by_xpath('//div[@class="selectbox-list"]').click()
self.driver.find_element_by_xpath("/html/body/div[2]/div/ul/li[4]").text
self.driver.find_element_by_xpath('//span[@class="mask2"]').click()
self.driver.find_element_by_xpath('//a[@class="_responsive_scrap_button

_click(nmp.front.sellershop.toggleKeep(4722827602)) _stopDefault _productPreLaunch N=a:pcs.mylist"]').click()
time.sleep(1)
except:
self.driver.get(self.ITEMURL)
self.driver.find_element_by_xpath("/html/body/div[2]/div/ul/li[4]").text
self.driver.find_element_by_xpath('//span[@class="mask2"]').click()
self.driver.find_element_by_xpath('//a[@class="_responsive_scrap_button

_click(nmp.front.sellershop.toggleKeep(4722827602)) _stopDefault _productPreLaunch N=a:pcs.mylist"]').click()
time.sleep(1)

def End(self):
sys.exit()

def test(self):
pass

def Mythread(self):
for i in range(1000000):
thread = Ui_MainWindow.threadclass(self)
thread.start()

class ThreadClass(QThread):
def __init__(self, parent = None):
super(ThreadClass, self).__init__(parent)

def run(self):
try:
self.driver.get(self.ITEMURL)
self.driver.find_element_by_xpath('//div[@class="selectbox-label"]').click()
self.driver.find_element_by_xpath('//div[@class="selectbox-list"]').click()
self.driver.find_element_by_xpath("/html/body/div[2]/div/ul/li[4]").text
self.driver.find_element_by_xpath('//span[@class="mask2"]').click()
self.driver.find_element_by_xpath('//a[@class="_responsive_scrap_button

_click(nmp.front.sellershop.toggleKeep(4722827602)) _stopDefault _productPreLaunch N=a:pcs.mylist"]').click()
time.sleep(1)
except:
self.driver.get(self.ITEMURL)
self.driver.find_element_by_xpath("/html/body/div[2]/div/ul/li[4]").text
self.driver.find_element_by_xpath('//span[@class="mask2"]').click()
self.driver.find_element_by_xpath('//a[@class="_responsive_scrap_button

_click(nmp.front.sellershop.toggleKeep(4722827602)) _stopDefault _productPreLaunch N=a:pcs.mylist"]').click()
time.sleep(1)

class Ui_MainWindow(QObject):
def __init__(self, parent = None):
self.threadclass = ThreadClass(self)

if __name__ == "__main__":
app = QApplication(sys.argv)
window = AutoBuy()
window.show()
app.exec_()



초심자로서 첫 응용이었고, 도움되는 글들을 찾아보면 다들 응용프로그램 제작을 통해서 실력을 키울 수 있다고 해서 만들어봤지만 결국 지금 봐도 어느 한줄 눈에 들어오고 이해되는게 없다. 프로그래머는 어떻게 프로그램을 짜는 것인지 부쩍 궁금하기도 하고, 재미는 있는데 독학에는 한계가 느껴지고, 문하생을 뽑는다고 하면 꼭 들어가고 싶다. 프로그래머의 뇌구조는 어떠한지, 이 분야는 내가 닿을 수 없는 분야인지 많은 생각을 하게 만든 시도였다.

+ Recent posts