diff --git a/data/weather_capture.py b/data/weather_capture.py index 05437c9..2ec33f2 100644 --- a/data/weather_capture.py +++ b/data/weather_capture.py @@ -7,46 +7,39 @@ from datetime import datetime import os import time import tempfile -from selenium import webdriver -from selenium.webdriver.chrome.options import Options +# 옵션 객체 생성 및 설정 options = Options() options.add_argument('--headless') options.add_argument('--window-size=1802,1467') +options.add_argument('--no-sandbox') # Docker 환경 권장 +options.add_argument('--disable-dev-shm-usage') # Docker 환경 권장 +options.add_argument('--disable-gpu') # GPU 사용 안함 (headless에서 권장) -# 임시 폴더 생성 후 user-data-dir로 지정 (매 실행마다 새 폴더 사용) +# 임시 사용자 데이터 디렉토리 지정 (매 실행마다 고유한 디렉토리 사용) temp_dir = tempfile.mkdtemp() options.add_argument(f'--user-data-dir={temp_dir}') -# Docker에서 권장하는 옵션 -options.add_argument('--no-sandbox') -options.add_argument('--disable-dev-shm-usage') - +# 드라이버 실행 driver = webdriver.Chrome(options=options) -# 현재 스크립트 경로 기준 저장 +# 현재 스크립트 위치 script_dir = os.path.dirname(os.path.abspath(__file__)) -# 브라우저 옵션 설정 -options = Options() -options.add_argument('--headless') -options.add_argument('--window-size=1802,1467') - -driver = webdriver.Chrome(options=options) driver.get('https://www.weather.go.kr/w/weather/forecast/short-term.do#dong/4148026200/37.73208578534846/126.79463099866948') wait = WebDriverWait(driver, 10) -# 첫 번째 탭 클릭 (안전하게 element_to_be_clickable 사용) +# 첫 번째 탭 클릭 (element_to_be_clickable 사용) tab_button = wait.until(EC.element_to_be_clickable( (By.XPATH, '//*[@id="digital-forecast"]/div[1]/div[3]/div[1]/div/div/a[2]') )) tab_button.click() -# 두 번째 항목 클릭 (stale element 대비해서 클릭 직전 다시 찾기) +# 두 번째 항목 클릭 (stale element 방지 위해 최대 3회 재시도) list_button_xpath = '//*[@id="digital-forecast"]/div[1]/div[3]/ul/div[1]/a[2]' -for attempt in range(3): # 최대 3번 재시도 +for attempt in range(3): try: list_button = wait.until(EC.element_to_be_clickable((By.XPATH, list_button_xpath))) list_button.click() @@ -59,14 +52,14 @@ else: driver.quit() exit(1) -time.sleep(2) # 클릭 후 페이지 반영 대기 +time.sleep(2) # 페이지 반영 대기 # 캡처 대상 요소 찾기 target_element = wait.until(EC.presence_of_element_located( (By.XPATH, '/html/body/div[2]/section/div/div[2]') )) -# 파일 저장 경로 설정 +# 저장 경로 설정 timestamp = datetime.now().strftime('%Y%m%d') save_path = os.path.join(script_dir, f'weather_capture_{timestamp}.png')