Files
fg-auto/data/weather_capture.py
2025-06-24 17:02:50 +09:00

43 lines
1.6 KiB
Python

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 datetime import datetime
import os
import time
# 현재 스크립트 경로 기준 저장
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')
time.sleep(5)
# 첫 번째 탭 클릭
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)
# 두 번째 항목 클릭
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)
# 캡처 대상 요소 찾기
target_element = driver.find_element(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') #날짜만 표시
save_path = os.path.join(script_dir, f'weather_capture_{timestamp}.png')
# 요소 스크린샷 저장
target_element.screenshot(save_path)
driver.quit()
print(f'📸 캡처 완료! 저장 위치: {save_path}')