파이썬으로 이메일 첨부파일 넣어 보내기

J.H.H.·2021년 9월 22일
0
post-thumbnail

2일차 미션

성공적

from openpyxl import Workbook

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome('chromedriver')

url = "https://search.naver.com/search.naver?where=news&sm=tab_jum&query=%EC%9E%AC%EB%82%9C%EC%A7%80%EC%9B%90%EA%B8%88"

driver.get(url)
req = driver.page_source
soup = BeautifulSoup(req, 'html.parser')

articles = soup.select('#main_pack > section > div > div.group_news > ul > li')

wb = Workbook()
ws1 = wb.active
ws1.title = "articles"
ws1.append(["제목", "링크", "썸네일"])

for article in articles:
title = article.select_one('div.news_wrap.api_ani_send > div > a')['title']
url = article.select_one('div.news_wrap.api_ani_send > div > a')['href']
press = article.select_one('div.news_wrap.api_ani_send > div > div.news_info > div.info_group > a.info.press').text.split(' ')[0].replace('언론사', ' ')
thumbnail = article.select_one('div.news_wrap.api_ani_send > a > img')['src']

ws1.append([title, url, thumbnail])

driver.quit()
wb.save(filename='articles.xlsx')


이메일 파이썬 구문

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders

보내는 사람 정보

me = "hhhhhh1227@gmail.com"
my_password = "!!!!!!"

로그인하기

s = smtplib.SMTP_SSL('smtp.gmail.com')
s.login(me, my_password)

받는 사람 정보

emails = ['asdfgh@naver.com', 'qwerty@naver.com']

for you in emails:

# 메일 기본 정보 설정
msg = MIMEMultipart('alternative')
msg['Subject'] = "파이썬_이메일_2일차"
msg['From'] = me
msg['To'] = you

# 메일 내용 쓰기
content = "파이썬 코딩연습 (이메일보내기) 2일차 실습숙제"
part2 = MIMEText(content, 'plain')
msg.attach(part2)

# 첨부파일 보내기
part = MIMEBase('application', "octet-stream")
with open("articles.xlsx", 'rb') as file:
    part.set_payload(file.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment", filename="재난지원금기사.xlsx")
msg.attach(part)

# 메일 보내고 서버 끄기
s.sendmail(me, you, msg.as_string())

s.quit()

profile
코린이를 넘어 데이터 시각화를 휘두르는(?) 그날까지

0개의 댓글

관련 채용 정보