λ°μ΄ν° ν¬λ‘€λ§ ν μ΄λ©μΌ 보λ΄κΈ°
λλμ μ 보λ₯Ό μ°Ύκ³ μ 리νλ λ° λΆνμν μκ°μ λλΉνκ³ μμ§ μμΌμ κ°μ?
μΈν°λ·μμ νμν μ 보λ₯Ό μλμΌλ‘ μμ§νκ³ κ°κ³΅νμ¬ μλ¦Ό λ©μΌμ 보λ΄λ μμ€ν μ ꡬμΆνλ©΄ μ 무 μμ°μ±μ ν¬κ² λμΌ μ μμ΅λλ€.
λ³λμ λ°μ΄ν° μμ§ μμ€ν μ΄λ μλ¦Ό μμ€ν μ ꡬμΆν νμκ° μκΈ° λλ¬Έμ λλ€.
μ΄λ² μμ
μμλ κΈ°μμ²μμ λ μ¨ μ 보
λ₯Ό κ°μ Έμ¨ ν, μμ§ν μ 보λ₯Ό μ΄λ©μΌ
λ‘ μ μ‘νλ λ°©λ²μ μμλ³΄κ² μ΅λλ€.
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from selenium import webdriver from selenium.webdriver.common.by import By # μ½λνλ μ¦ κ°μ μ΄λ©μΌλ§ μμ κ°λ₯ receiver_email = "youremail@example.com" # κΈ°μμ²μμ λ μ¨ μ 보 κ°μ Έμ€κΈ° def get_weather(): # Chrome λλΌμ΄λ² μ€ν driver = webdriver.Chrome() # κΈ°μμ² λ μ¨ μ 보 νμ΄μ§λ‘ μ΄λ url = "https://www.weather.go.kr/w/index.do" driver.get(url) # κΈ°μ¨κ³Ό 체κ°μ¨λ μ 보 κ°μ Έμ€κΈ° temperature_element = driver.find_element(By.CLASS_NAME, 'tmp') feels_like_element = driver.find_element(By.CLASS_NAME, 'chill') # κΈ°μ¨κ³Ό 체κ°μ¨λ μ 보 μ μ₯ temperature = temperature_element.text feels_like = feels_like_element.text driver.quit() # ννλ‘ κΈ°μ¨κ³Ό 체κ°μ¨λ μ 보 λ°ν weather_info = (temperature, feels_like) return weather_info # μ΄λ©μΌλ‘ λ μ¨ μ 보 μ μ‘ def send_email(weather_info): sender_email = "admin_email" sender_password = "admin_password" smtp_server = "smtp.gmail.com" port = 587 # μ΄λ©μΌ κ΅¬μ± msg = MIMEMultipart('alternative') msg['Subject'] = "μ€λμ λ μ¨ μ 보" msg['From'] = sender_email msg['To'] = receiver_email body = f""" <html> <body> <h1>μ€λμ λ μ¨ μ 보</h1> <p>κΈ°μ¨: {weather_info[0]}</p> <p>체κ°μ¨λ: {weather_info[1]}</p> </body> </html> """ # MIME ννΈ μΆκ° html_email = MIMEText(body, 'html') msg.attach(html_email) # SMTP μλ² μ°κ²° server = smtplib.SMTP(smtp_server, port) # TLS(μ΄λ©μΌ μνΈν) μ°κ²° server.starttls() # μ΄λ©μΌ λ‘κ·ΈμΈ server.login(sender_email, sender_password) # μ΄λ©μΌ λ©μμ§λ₯Ό λ¬Έμμ΄λ‘ λ³ν text = msg.as_string() try: # μ΄λ©μΌ μ μ‘ server.sendmail(sender_email, receiver_email, text) print("μ΄λ©μΌμ΄ μ±κ³΅μ μΌλ‘ λ°μ‘λμμ΅λλ€!") except Exception as e: print(f"μ΄λ©μΌ λ°μ‘ μ€ μλ¬ λ°μ: {e}") finally: server.quit() # λ μ¨ μ 보 κ°μ Έμ€κΈ° weather_info = get_weather() # μ΄λ©μΌ μ μ‘ send_email(weather_info)
μ μ½λλ Seleniumμ νμ©ν΄ μΉμμ λ°μ΄ν°λ₯Ό ν¬λ‘€λ§νκ³ , μμ§ν λ°μ΄ν°λ₯Ό μ΄λ©μΌλ‘ μ μ‘ν©λλ€.
μ΄λ¬ν κ³Όμ μ schedule
λΌμ΄λΈλ¬λ¦¬λ₯Ό νμ©ν΄ μ£ΌκΈ°μ μΌλ‘ μ€ννλ©΄, λ§€μΌ νΉμ μκ°μ λ μ¨ μ 보λ₯Ό μ΄λ©μΌλ‘ λ°μλ³Ό μ μμ΅λλ€.
import schedule # schedule λΌμ΄λΈλ¬λ¦¬λ₯Ό μ¬μ©ν΄ 1μκ°λ§λ€ λ μ¨ μ 보 μ΄λ©μΌ μ μ‘ schedule.every().hour.do(send_email, get_weather()) # 보μμ μ΄μ λ‘ μ€μ΅ νκ²½μμλ μ£ΌκΈ°μ μ΄λ©μΌ λ°μ‘μ΄ μλνμ§ μμ΅λλ€ while True: schedule.run_pending()
κΈ°μμ²μ λ μ¨ μ 보μ κ°μ΄ λμ μΈ λ°μ΄ν°λ₯Ό ν¬λ‘€λ§ν λ νμν λΌμ΄λΈλ¬λ¦¬λ 무μμΈκ°μ?
requests
beautifulsoup4
selenium
smtplib
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result