0. ๋ค์ด๊ฐ๊ธฐ ์
๐ ๏ธ ์์ ์ ์์
- Python ํ๋ก๊ทธ๋จ ์์ฑํฉ๋๋ค.
- crontab์ ์์ ๋ฑ๋กํฉ๋๋ค.
โ
Github Actions vs crontab(Windows: ์์
์ค์ผ์ค๋ฌ)
๊ณ์ ์ ๋ณด๋ฅผ ํน์ ์๋ฒ์ ์ฌ๋ฆฌ๋ ๊ฒ์ด ๋ถ์ํด์ crontab์ ์ฌ์ฉํ๊ธฐ๋ก ๊ฒฐ์ ํ์ต๋๋ค.
1. Python ํ๋ก๊ทธ๋จ ์์ฑ
1.1 Anaconda ์ค์น ๋ฐ ํ๊ฒฝ๊ตฌ์ฑ
-
์๋ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ Anaconda ์ค์น ํ์ด์ง๋ก ์ด๋ํฉ๋๋ค.
๐Anaconda
macOS ์ค์น ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ Install for me only ์ต์ ์ผ๋ก ์ค์น๋ฅผ ์งํํฉ๋๋ค.
๐Installing-on-macOS -
๊ฐ์ํ๊ฒฝ์ ์์ฑํฉ๋๋ค.
(base) edward@Edwards-MacBook-Pro conda create -n ENVNAME python=VERSION (base) edward@Edwards-MacBook-Pro conda activate ENVNAME (ENVNAME) edward@Edwards-MacBook-Pro pip install jupyter (ENVNAME) edward@Edwards-MacBook-Pro pip install nbconvert (ENVNAME) edward@Edwards-MacBook-Pro pip install selenium
-
Jupyter notebook์ ์คํํ์ฌ ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค.
๋ฒํธ๋ณ ํต๊ณ ์ ๋ณด๋ฅผ ํฌ๋กค๋ง(๐๋ฒํธ๋ณ ํต๊ณ)ํ์ฌ ๋ฒํธ๋ณ๋ก ๊ฐ์ค์น๋ฅผ ๋ถ์ฌํ๊ณ , ๊ฐ์ค์น๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ฒํธ๋ฅผ ์ ํํฉ๋๋ค.
โ๏ธ Account Info ์ฃผ์ ํ๋จ์ ๊ณ์ ์ ๋ณด๋ฅผ ์ ๋ ฅํ์ธ์.(ENVNAME) edward@Edwards-MacBook-Pro jupyter notebook
import random from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as ec from selenium.webdriver.common.keys import Keys ############################### # Classes ############################### class Ball: def __init__(self, n, w): self.number = n self.weight = w ############################### # Functions ############################### # Common def wait_and_click(driver, xpath): element = WebDriverWait(driver, 10).until( ec.element_to_be_clickable((By.XPATH, xpath)) ).click() def close_pop_ups(main_window): new_popup = WebDriverWait(driver, 3).until(ec.new_window_is_opened(main_window)) windows = driver.window_handles for w in windows: if w != main_window[0]: driver.switch_to.window(w) driver.close() driver.switch_to.window(main_window[0]) # Lotto def get_num_arr(balls, total_weight): result = [] while result.__len__() != 6: r = random.random() for b in balls: r -= b.weight if r < 0 and b not in result: result.append(b) break return result ############################### # Driver Options Settings ############################### options = Options() options.add_argument("headless") options.add_argument("--disable-loging") options.add_argument("--blink-settings=imagesEnabled=false") # Unloading Images ############################## # Account Info ############################## USER_ID = "user_id" USER_PW = "user_pw" ############################## # Create Session ############################## driver = webdriver.Chrome(options) driver.get("https://dhlottery.co.kr/user.do?method=login&returnUrl=") main_window = driver.window_handles logging.debug("[Main window name is %s..;]" %main_window) print("[Main window name is %s..;]" %main_window) print("[Create Session Completed..;]") ############################## # Login Process ############################## user_id = "/html/body/div[3]/section/div/div[2]/div/form/div/div[1]/fieldset/div[1]/input[1]" user_pw = "/html/body/div[3]/section/div/div[2]/div/form/div/div[1]/fieldset/div[1]/input[2]" wait_and_click(driver, user_id) driver.find_element(By.XPATH, user_id).send_keys(USER_ID) driver.find_element(By.XPATH, user_pw).send_keys(USER_PW, Keys.ENTER) print("[Login Completed..;]") # Close Pop-ups close_pop_ups(main_window) ############################## # Select Numbers ############################## driver.get("https://dhlottery.co.kr/gameResult.do?method=statByNumber") weights = driver.find_elements(By.XPATH, "//*[@id='printTarget']/tbody/tr/td[3]") balls = [] total_weight = 0 for i in range(0, 45): w = int(weights[i].text) balls.append(Ball(i + 1, w)) total_weight += w for b in balls: b.weight = b.weight / total_weight ############################## # Buy 6/45 Lotto ############################## wait_and_click(driver, "//*[@id=\"gnb\"]/ul/li[1]/a") driver.find_element(By.XPATH, "//*[@id=\"gnb\"]/ul/li[1]/div/ul/li[1]/a").click() # Switch Window windows = driver.window_handles for w in windows: if w != main_window[0]: driver.switch_to.window(w) driver.switch_to.frame("ifrm_tab") # Execute Script script = "" for i in range(0, 5): selected_balls = get_num_arr(balls, total_weight) for ball in selected_balls: script += " $('#check645num%s').click();\n " %ball.number script += " $('#btnSelectNum').click();\n " script += " $('#btnBuy').click();\n " driver.execute_script(script) wait_and_click(driver, "/html/body/div[4]/div/div[2]/input[1]") print("[Complete All tasks]") driver.quit()
-
์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๊ธฐ ์ํด์ .ipynb์ .py๋ก ๋ณํํฉ๋๋ค.
(ENVNAME) edward@Edwards-MacBook-Pro jupyter nbconvert --to script FILENAME.ipynb
2. crontab ํธ์ง
๋ค์ ๋ช
๋ น์ด๋ฅผ ์คํํ์ฌ Python ์คํ ๊ฒฝ๋ก๋ฅผ ์ป์ต๋๋ค.
(ENVNAME) edward@Edwards-MacBook-Pro which python
๊ทธ๋ฆฌ๊ณ ์๋ ๋ช
๋ น์ด๋ฅผ ํตํด์ crontab์ ํธ์งํฉ๋๋ค.
(ENVNAME) edward@Edwards-MacBook-Pro sudo crontab -e
i
๋ฅผ ์
๋ ฅํ๋ฉด ํธ์ง ๋ชจ๋๋ก ๋ณ๊ฒฝ๋ฉ๋๋ค.
Cron Schedule ํํ์ ์์ฑ ํ Python ์คํ ๊ฒฝ๋ก๋ฅผ ๋ถ์ฌ๋ฃ์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์คํํ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์
๋ ฅํฉ๋๋ค.
esc
๋๋ฅด๋ฉด ํธ์ง ๋ชจ๋์์ ๋์ค๊ฒ ๋๋ฉฐ :
๋ฅผ ์
๋ ฅํ ๋ค wq
(์ ์ฅ ๋ฐ ์ข
๋ฃ)๋ฅผ ์
๋ ฅํ์ฌ ํธ์ง์ ๋ง๋ฌด๋ฆฌํฉ๋๋ค.
# _______________ minute (0 - 59)
# | _______________ hour (0 - 23)
# | | _______________ day of the month (1 - 31)
# | | | _______________ month (1 - 12)
# | | | | _______________ day of the week (0 - 7)
# | | | | |
# * * * * * command to execute
# ๊ธ์์ผ ์คํ 9์์ ์คํ
0 9 * * 5 anaconda3/envs/ENVNAME/bin/python /Users/edward/auto/lotto.py