기사 웹스크래핑(크롤링)&엑셀 파일 이메일로 보내기

2020. 10. 2. 22:15·Python

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')
Colored by Color Scripter
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()
Colored by Color Scripter
cs

'Python' 카테고리의 다른 글

이미지 웹 스크래핑(크롤링)  (0) 2020.10.02
<변수&타입&조건문&함수>  (0) 2020.09.19
'Python' 카테고리의 다른 글
  • 이미지 웹 스크래핑(크롤링)
  • <변수&타입&조건문&함수>
switch_user
switch_user
나의 공부 기록
  • switch_user
    while(true)
    switch_user
  • 전체
    오늘
    어제
    • 분류 전체보기
      • C
      • C++
      • Java
      • Python
      • Web
      • Security
        • Web Hacking
        • Reverse Engineering
      • DB
      • Machine Learning
      • MCP
      • Computer Science
      • Linux
      • Algorithm
      • 진로
      • 기타
  • 블로그 메뉴

    • 홈
    • 태그
    • velog
    • Github
  • 링크

    • velog
    • Github
  • 공지사항

  • 인기 글

  • 태그

    cin.getline
    어셈블리어
    HTML
    비트연산
    사례 기반 학습
    xss
    리버싱
    웹해킹
    반복문
    배치 학습
    생성자와 소멸자
    모델 기반 학습
    Web 기초
    머신러닝
    쿠키
    IDA
    인터프리팅
    x64dbg
    디컴파일
    SQLi
    CSS
    어셈블리
    HTTP
    클래스 외부에 함수 구현
    웹
    ml
    Hacking Process
    SQL
    코드 패치
    race condition
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
switch_user
기사 웹스크래핑(크롤링)&엑셀 파일 이메일로 보내기
상단으로

티스토리툴바