2일차 미션
성공적
from openpyxl import Workbook
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome('chromedriver')
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()