새로새록
인테넷 자동화 연습 -크롤링바탕 본문
크롤링 -> 이미지 저장하기
import dload
from bs4 import BeautifulSoup
from selenium import webdriver
import time
driver = webdriver.Chrome('chromedriver') # 웹드라이버 파일의 경로
driver.get("https://search.daum.net/search?w=img&nil_search=btn&DA=NTB&enc=utf8&q=%EC%95%84%EC%9D%B4%EC%9C%A0")
time.sleep(5) # 5초 동안 페이지 로딩 기다리기
req = driver.page_source
soup = BeautifulSoup(req, 'html.parser')
thumbnails = soup.select("#imgList > div > a > img")
i=1
for thumbnail in thumbnails:
src = thumbnail["src"]
dload.save(src, f'imgs/{i}.jpg')
i+=1
driver.quit() # 끝나면 닫아주기
텍스트 크롤링 ->엑셀로 저장하기
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')
articles = soup.select('#main_pack > section.sc_new.sp_nnews._prs_nws > 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').text
url = article.select_one('div.news_wrap.api_ani_send > div > a')['href']
comp = article.select_one('div.news_info > div.info_group > a.info.press').text.split(' ')[0].replace('언론사', ' ')
ws1.append([title, url, comp])
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 = "보내는사람@gmail.com"
my_password = "비밀번호"
# 로그인하기
s = smtplib.SMTP_SSL('smtp.gmail.com')
s.login(me, my_password)
# 받는 사람 정보
you = "받는사람@아무_도메인"
# 메일 기본 정보 설정
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()
>2단계 인증 해제
https://myaccount.google.com/signinoptions/two-step-verification
>보안 수준이 낮은 앱 해제하기
https://myaccount.google.com/lesssecureapps
>여러 사람에게 이메일 보내기 시작코드
# 받는 사람 정보
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()
파일 첨부하기 (메일보내기 앞에)
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)
* 스파르타 를 참고했음
'글 > 리뷰' 카테고리의 다른 글
python - tkinter : gui기본 기능 연습 (0) | 2021.09.25 |
---|---|
워드클라우드 (0) | 2021.09.19 |
pandas - 인코딩안될때, utf-8 vs. cp949 (0) | 2021.09.14 |
TypeError: no numeric data to plot [ pandas object(콤마있는 문자열) int(숫자)로 전환하기 ] (0) | 2021.09.13 |
청년저축계좌 / 청년내일채움공제 /지원대상과 신청방법 (0) | 2021.07.17 |