diff --git a/data/weather_capture.py b/data/weather_capture.py index df5cd5b..ebc0916 100644 --- a/data/weather_capture.py +++ b/data/weather_capture.py @@ -1,14 +1,11 @@ from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By -from selenium.webdriver.common.action_chains import ActionChains +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC from datetime import datetime import os import time -import tempfile - -# 크롬 프로세스 정리 -os.system("pkill chrome || true") # 현재 스크립트 경로 기준 저장 script_dir = os.path.dirname(os.path.abspath(__file__)) @@ -16,36 +13,39 @@ script_dir = os.path.dirname(os.path.abspath(__file__)) # 브라우저 옵션 설정 options = Options() options.add_argument('--headless') +options.add_argument('--disable-gpu') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') -options.add_argument('--disable-gpu') -options.add_argument('--remote-debugging-port=9222') -options.add_argument(f'--user-data-dir={tempfile.mkdtemp()}') # 고유한 프로필 경로 +# 드라이버 실행 driver = webdriver.Chrome(options=options) +driver.set_window_size(1802, 1467) # 명시적으로 창 크기 설정 + +# 날씨 페이지 접속 driver.get('https://www.weather.go.kr/w/weather/forecast/short-term.do#dong/4148026200/37.73208578534846/126.79463099866948') -time.sleep(5) + +wait = WebDriverWait(driver, 10) # 첫 번째 탭 클릭 -tab_button = driver.find_element(By.XPATH, '//*[@id="digital-forecast"]/div[1]/div[3]/div[1]/div/div/a[2]') -ActionChains(driver).move_to_element(tab_button).click().perform() -time.sleep(2) +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() # 두 번째 항목 클릭 -list_button = driver.find_element(By.XPATH, '//*[@id="digital-forecast"]/div[1]/div[3]/ul/div[1]/a[2]') -ActionChains(driver).move_to_element(list_button).click().perform() -time.sleep(2) +list_button = wait.until(EC.element_to_be_clickable( + (By.XPATH, '//*[@id="digital-forecast"]/div[1]/div[3]/ul/div[1]/a[2]'))) +list_button.click() # 캡처 대상 요소 찾기 -target_element = driver.find_element(By.XPATH, '/html/body/div[2]/section/div/div[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_%H%M') #시간까지 표시 -timestamp = datetime.now().strftime('%Y%m%d') #날짜만 표시 +timestamp = datetime.now().strftime('%Y%m%d') # 날짜만 표시 save_path = os.path.join(script_dir, f'weather_capture_{timestamp}.png') # 요소 스크린샷 저장 target_element.screenshot(save_path) driver.quit() -print(f'📸 캡처 완료! 저장 위치: {save_path}') \ No newline at end of file +print(f'📸 캡처 완료! 저장 위치: {save_path}')