2일차 오늘 배우고 나서 만들어진 산출물
크롬 - 개발자모드에서 HTML 구조를 파악한다.
신문기사 제목 가져오기
articles = soup.select("#main_pack > div.news.mynews.section._prs_nws > ul > li")
for article in articles:
a_tag = article.select_one("dl > dt > a")
print(a_tag.text)
신문기사 URL 주소와 신문사 가져오기
url = a_tag["href"]
title = a_tag.text
comp = article.select_one("dd.txt_inline > span._sp_each_source").text.split(" ")[0].replace('언론사','')
print(title, url, comp)
완성된 소스
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=추석"
driver.get(url)
req = driver.page_source
soup = BeautifulSoup(req, 'html.parser')
articles = soup.select('#main_pack > div.news.mynews.section._prs_nws > ul > li')
for article in articles:
a_tag = article.select_one('dl > dt > a')
title = a_tag.text
url = a_tag['href']
comp = article.select_one('dd.txt_inline > span._sp_each_source').text.split(' ')[0].replace('언론사','')
print(title, url, comp)
driver.quit()
파이썬에서 엑셀을 쓰고 읽기 위해 openpyxl 패키지를 설치한다.
기사를 읽어 엑셀파일로 저장하는 코드
from bs4 import BeautifulSoup
from selenium import webdriver
from openpyxl import Workbook
driver = webdriver.Chrome('chromedriver')
url = "https://search.naver.com/search.naver?where=news&sm=tab_jum&query=추석"
driver.get(url)
req = driver.page_source
soup = BeautifulSoup(req, 'html.parser')
wb = Workbook()
ws1 = wb.active
ws1.title = "articles"
ws1.append(["제목", "링크", "신문사"])
articles = soup.select('#main_pack > div.news.mynews.section._prs_nws > ul > li')
for article in articles:
a_tag = article.select_one('dl > dt > a')
title = a_tag.text
url = a_tag['href']
comp = article.select_one('dd.txt_inline > span._sp_each_source').text.split(' ')[0].replace('언론사','')
ws1.append([title, url, comp])
driver.quit()
wb.save(filename='articles.xlsx')
내장패키지인 smtplib 패키지 사용해서 smtp를 이용 이메일을 전송할 수 있다.
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 = "보내는사람@gmail.com"
my_password = "비밀번호"
# 로그인하기
s = smtplib.SMTP_SSL('smtp.gmail.com')
s.login(me, my_password)
# 받는 사람 정보
email_list = ["이메일1", "이메일2"]
for you in email_list:
# 메일 기본 정보 설정
msg = MIMEMultipart('alternative')
msg['Subject'] = "제목"
msg['From'] = me
msg['To'] = you
# 메일 내용 쓰기
content = "메일 내용"
part2 = MIMEText(content, 'plain')
msg.attach(part2)
# 메일 보내기
s.sendmail(me, you, msg.as_string())
# 다 끝나고 닫기
s.quit()
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 = "보내는사람@gmail.com"
my_password = "비밀번호"
# 로그인하기
s = smtplib.SMTP_SSL('smtp.gmail.com')
s.login(me, my_password)
# 받는 사람 정보
email_list = ["이메일1", "이메일2"]
for you in email_list:
# 메일 기본 정보 설정
msg = MIMEMultipart('alternative')
msg['Subject'] = "제목"
msg['From'] = me
msg['To'] = you
# 메일 내용 쓰기
content = "메일 내용"
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()
from bs4 import BeautifulSoup
from selenium import webdriver
from openpyxl import Workbook
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
driver = webdriver.Chrome('chromedriver')
url = "https://search.naver.com/search.naver?where=news&sm=tab_jum&query=추석"
driver.get(url)
req = driver.page_source
soup = BeautifulSoup(req, 'html.parser')
wb = Workbook()
ws1 = wb.active
ws1.title = "articles"
ws1.append(["제목", "링크", "신문사", "썸네일"])
articles = soup.select('#main_pack > section.sc_new.sp_nnews._prs_nws > div > div.group_news > ul > li')
for article in articles:
#print(article)
a_tag = article.select_one('div.news_wrap.api_ani_send > div > a')
title = a_tag.text
url = a_tag['href']
comp = 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, comp, thumbnail])
driver.quit()
wb.save(filename='articles.xlsx')
# 보내는 사람 정보
me = "memilmook@gmail.com"
my_password = "password"
# 로그인하기
s = smtplib.SMTP_SSL('smtp.gmail.com')
s.login(me, my_password)
# 받는 사람 정보
you = "memilmook@naver.com"
# 메일 기본 정보 설정
msg = MIMEMultipart('alternative')
msg['Subject'] = "기사를 받아라"
msg['From'] = me
msg['To'] = you
# 메일 내용 쓰기
content = "기사 보냄"
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="./articles.xlsx")
msg.attach(part)
# 메일 보내고 서버 끄기
s.sendmail(me, you, msg.as_string())
s.quit()