news.py : 기사 웹 스크래핑 후 엑셀 파일로 저장
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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=추석"
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:
title = article.select_one('dl > dt > a').text
url = article.select_one('dl > dt > a')['href']
comp = article.select_one('span._sp_each_source').text.split(' ')[0].split('언론사')[0]
thumbnail = article.select_one('div > a > img')['src']
ws1.append([title, url, comp, thumbnail])
driver.quit()
wb.save(filename='articles.xlsx')
|
cs |
myemail.py : 저장된 엑셀 파일 메일로 보내기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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 = "mspark010506@gmail.com"
my_password = "pilot3351719"
# 로그인하기
s = smtplib.SMTP_SSL('smtp.gmail.com')
s.login(me, my_password)
# 받는 사람 정보
emails = ['mspark010506@gmail.com', 'mspark010506@naver.com']
for you in emails:
# 메일 기본 정보 설정
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()
|
cs |
'Python' 카테고리의 다른 글
이미지 웹 스크래핑(크롤링) (0) | 2020.10.02 |
---|---|
<변수&타입&조건문&함수> (0) | 2020.09.19 |