๋ค์ด๋ฒ ์ด๋ฉ์ผ ์๋ํ ํ๋ก๊ทธ๋จ
๋ค์ด๋ฒ์ ์๋์ผ๋ก ๋ก๊ทธ์ธํด์, ์ค๋น๋ ์ด๋ฉ์ผ์ ์๋์ผ๋ก ๋ณด๋ด๋ ํ๋ก๊ทธ๋จ
ํ๋ก๊ทธ๋จ
1
2
ํ๋กฌํํธ
ํ๋ฅด์๋
- ํ์ด์ฌ ํ๋ก๊ทธ๋๋ฐ ํ์ต์
์์
- ๋ค์ด๋ฒ ์ด๋ฉ์ผ ์๋ํ ํ๋ก๊ทธ๋จ ๊ฐ๋ฐ
๋งฅ๋ฝ
- ์ธ์ด : ํ์ด์ฌ
- ๋ฒ์ : 3.13
- ๊ตฌ์กฐ : GUI ํ๋ก๊ทธ๋จ
- ๊ธฐ๋ฅ
* MySQL ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ด๋ฉ์ผ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ
- ๋ฐ๋์ฌ๋ ๋ฉ์ผ์ฃผ์, ์ ๋ชฉ, ๋ด์ฉ, ์ํ(์๋ฃ,์ ์ก์ )
- ํ๋ก๊ทธ๋จ ์คํ ์, email ํ
์ด๋ธ ์์ผ๋ช
์์ฑ
- database=python, useranme=python, password=123456
* ์ผ๊ด์ ์ก ๊ธฐ๋ฅ
- "์ผ๊ด์ ์ก" ๋ฒํผ์ ๋๋ฅด๋ฉด, ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ด๋ฉ์ผ ์ ๋ณด๋ฅผ ์กฐํํ์ฌ
์์ฐจ์ ์ผ๋ก ๋ฉ์ผ์ ์ ์กํ๋ ๊ธฐ๋ฅ
- ๋ก๊ทธ์ธ ์ฌ๋ถ ํ์ธ ํ, ๋ฉ์ผ ์ฐ๊ธฐ ์งํ
- ์ด๋ฉ์ผ ์ฐ๊ธฐ url : https://mail.naver.com/v2/new
- ์
๋ ฅ ํ๊ทธ
* ์ด๋ฉ์ผ : id="recipient_input_element"
* ์ ๋ชฉ : id="subject_title"
* ๋ด์ฉ : class="workseditor-content"
- ์ ์ก ๋ฒํผ
* class="button_write_task"
- ์ ์ก ํ,
https://mail.naver.com/v2/new/done ๊ฒฝ๋ก๋ก ์ด๋๋ ๋๊น์ง ๋๊ธฐ
https://mail.naver.com/v2/new/done ์๋ฃ ํ์ด์ง๋ก ์ด๋ ๋๋ฉด
ํด๋น ์ด๋ฉ์ผ ์ ๋ณด๋ DB์์ status ์๋ฃ๋ก ๋ณ๊ฒฝํ,
์๋ฃ๊ฐ ์๋ ๋ค์ ์ด๋ฉ์ผ ๋ณด๋ด๊ธฐ ์งํ
- ๋ชจ๋ ์ด๋ฉ์ผ ์ ์ก์ด ์๋ฃ๋๋ฉด, ํ์
์ฐฝ์ผ๋ก ์๋ด ๋ฉ์์ง ์ถ๋ ฅ
* ๋ค์ด๋ฒ ์๋ ๋ก๊ทธ์ธ ๊ธฐ๋ฅ
- ์์ด๋, ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด๋๊ณ ๋ก๊ทธ์ธ ๋ฒํผ ํด๋ฆญ ์, ๋ค์ด๋ฒ์ ๋ก๊ทธ์ธ
url : https://nid.naver.com/nidlogin.login?mode=form
ํ์
- ํ์ด์ฌ GUI ํ๋ก๊ทธ๋จ์์ ํฌ๋กฌ ์น๋ธ๋ผ์ฐ์ ๋ฅผ ์ ์ดํ๋ ๋ฐฉ์
์์
- ์์ด๋ ๋น๋ฒ ์
๋ ฅ ํ, ๋ก๊ทธ์ธ ๋ฒํผ ํด๋ฆญ ์ ๋ธ๋ผ์ฐ์ ์์ ๋ค์ด๋ฒ์ ๋ก๊ทธ์ธ
- ๋ฉ์ผ ๋ฑ๋ก ๋ฒํผ ํด๋ฆญ ํ, ์ด๋ฉ์ผ์ฃผ์, ์ ๋ชฉ, ๋ด์ฉ ์์ฑ ํ ์ ์ฅํ๋ฉด
email ํ
์ด๋ธ์ ์ด๋ฉ์ผ ์ ๋ณด ๋ฑ๋ก
- ๋ฉ์ผ ์ ๋ณด ์ญ์ , ์์ ์ฒ๋ฆฌ
- "์ผ๊ด ์ ์ก" ๋ฒํผ ํด๋ฆญ ์, ์ฝ์ด๋๋ฆฐ ์ด๋ฉ์ผ ๋ชฉ๋ก์ ์์ฐจ์ ์ผ๋ก ์ ์ก
์บ๋ฒ์ค ๋ชจ๋๋ก ์๋ต
Plain Text
๋ณต์ฌ
์ฝ๋
import tkinter as tk
from tkinter import messagebox, simpledialog, Toplevel, Label, Entry, Text, Button, END
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import mysql.connector
import time
# DB ์ค์
DB_CONFIG = {
'host': 'localhost',
'user': 'python',
'password': '123456',
'database': 'python'
}
# email ํ
์ด๋ธ ์์ฑ ํจ์
def init_db():
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS email (
id INT AUTO_INCREMENT PRIMARY KEY,
recipient VARCHAR(255),
subject VARCHAR(255),
content TEXT,
status VARCHAR(50) DEFAULT '์ ์ก์ '
)
""")
conn.commit()
cursor.close()
conn.close()
# ์ด๋ฉ์ผ ๋ฑ๋ก ํจ์
def add_email(recipient, subject, content):
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
cursor.execute("INSERT INTO email (recipient, subject, content) VALUES (%s, %s, %s)", (recipient, subject, content))
conn.commit()
cursor.close()
conn.close()
# ์ด๋ฉ์ผ ์์ ํจ์
def update_email(email_id, recipient, subject, content):
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
cursor.execute("""
UPDATE email SET recipient=%s, subject=%s, content=%s WHERE id=%s
""", (recipient, subject, content, email_id))
conn.commit()
cursor.close()
conn.close()
# ์ด๋ฉ์ผ ์ญ์ ํจ์
def delete_email(email_id):
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
cursor.execute("DELETE FROM email WHERE id=%s", (email_id,))
conn.commit()
cursor.close()
conn.close()
# ์ด๋ฉ์ผ ๋ฆฌ์คํธ ๊ฐ์ ธ์ค๊ธฐ (์ ์ก์ ์ํ๋ง)
def get_pending_emails():
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor(dictionary=True)
cursor.execute("SELECT * FROM email WHERE status='์ ์ก์ '")
emails = cursor.fetchall()
cursor.close()
conn.close()
return emails
# ์ํ ์
๋ฐ์ดํธ ํจ์
def mark_email_as_sent(email_id):
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
cursor.execute("UPDATE email SET status='์๋ฃ' WHERE id=%s", (email_id,))
conn.commit()
cursor.close()
conn.close()
# ์ด๋ฉ์ผ ์ ์ก ํจ์
def send_emails(driver):
emails = get_pending_emails()
for email in emails:
driver.get("https://mail.naver.com/v2/new")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "recipient_input_element"))).send_keys(email['recipient'])
driver.find_element(By.ID, "subject_title").send_keys(email['subject'])
# ๋ด์ฉ ์
๋ ฅ์ innerHTML ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ# ๋ด์ฉ ์
๋ ฅ์ innerHTML ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ (์ฒซ ๋ฒ์งธ ์์๋ก ์ง์ )
# content_elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "workseditor-content")))
# if content_elements:
# driver.execute_script("arguments[0].innerHTML = arguments[1];", content_elements[0], email['content'].replace('\n', '<br>'))
driver.find_element(By.CLASS_NAME, "button_write_task").click()
WebDriverWait(driver, 10).until(EC.url_contains("/v2/new/done"))
mark_email_as_sent(email['id'])
time.sleep(1)
messagebox.showinfo("์๋ฃ", "๋ชจ๋ ์ด๋ฉ์ผ ์ ์ก์ ์๋ฃํ์ต๋๋ค.")
# ๋ก๊ทธ์ธ ํจ์
def login_to_naver(driver, user_id, user_pw):
driver.get("https://nid.naver.com/nidlogin.login?mode=form")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "id"))).send_keys(user_id)
driver.find_element(By.ID, "pw").send_keys(user_pw)
driver.find_element(By.ID, "log.login").click()
# GUI ํ๋ก๊ทธ๋จ
class EmailApp:
def __init__(self, root):
self.root = root
self.root.title("๋ค์ด๋ฒ ์ด๋ฉ์ผ ์๋ํ")
tk.Label(root, text="์์ด๋").grid(row=0, column=0)
self.entry_id = tk.Entry(root)
self.entry_id.grid(row=0, column=1)
tk.Label(root, text="๋น๋ฐ๋ฒํธ").grid(row=1, column=0)
self.entry_pw = tk.Entry(root, show='*')
self.entry_pw.grid(row=1, column=1)
tk.Button(root, text="๋ก๊ทธ์ธ", command=self.login).grid(row=2, column=0, columnspan=2)
tk.Button(root, text="๋ฉ์ผ ๋ฑ๋ก", command=self.register_email).grid(row=3, column=0, columnspan=2)
tk.Button(root, text="์ผ๊ด ์ ์ก", command=self.bulk_send).grid(row=4, column=0, columnspan=2)
self.driver = None
def login(self):
user_id = self.entry_id.get()
user_pw = self.entry_pw.get()
self.driver = webdriver.Chrome()
login_to_naver(self.driver, user_id, user_pw)
def register_email(self):
def save():
recipient = entry_recipient.get()
subject = entry_subject.get()
content = text_content.get("1.0", END).strip()
if recipient and subject and content:
add_email(recipient, subject, content)
popup.destroy()
popup = Toplevel(self.root)
popup.title("๋ฉ์ผ ๋ฑ๋ก")
Label(popup, text="๋ฐ๋์ฌ๋").grid(row=0, column=0)
entry_recipient = Entry(popup, width=40)
entry_recipient.grid(row=0, column=1)
Label(popup, text="์ ๋ชฉ").grid(row=1, column=0)
entry_subject = Entry(popup, width=40)
entry_subject.grid(row=1, column=1)
Label(popup, text="๋ด์ฉ").grid(row=2, column=0)
text_content = Text(popup, width=40, height=10)
text_content.grid(row=2, column=1)
Button(popup, text="์ ์ฅ", command=save).grid(row=3, column=0, columnspan=2)
def bulk_send(self):
if not self.driver:
messagebox.showerror("์ค๋ฅ", "๋จผ์ ๋ก๊ทธ์ธ์ ํด์ฃผ์ธ์.")
return
send_emails(self.driver)
if __name__ == '__main__':
init_db()
root = tk.Tk()
app = EmailApp(root)
root.mainloop()
Python
๋ณต์ฌ