이를 하기 위해서는 구글 내 계정에서 앱 비밀번호를 설정해주어야 합니다.
- Windows 컴퓨터용 앱 비밀번호 생성 : 2단계 인증 사용 > 앱 비밀번호 > 메일, Windows 컴퓨터 > 생성
1) 크롤링을 위한 라이브러리 설치
import time
from urllib.request import urlopen
from bs4 import BeautifulSoup
2) 메일 발송을 위한 라이브러리 설치 및 함수 생성
# SMTP + Lib <- HTTP
# 서버를 이용해 메일을 원하는 곳으로 보낼 수 있도록 하는 라이브러리
import smtplib
# 전자우편을 위한 인터넷 표준 포맷
from email.mime.text import MIMEText
def sendMail(sender, receiver, msg):
smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465) # 구글 메일 서버에 접근
smtp.login(sender, '위에서 생성한 앱 비밀번호 입력') # 16글자의 [구글 계정 앱 비밀번호] 입력해주세요.
msg = MIMEText(msg) # 본문
msg['Subject'] = 'Product is available!' # 제목
smtp.sendmail(sender, receiver, msg.as_string()) # 메일 보내기
smtp.quit()
3) HTTP Response 얻기 & HTML source 얻기 & HTML 태그 꺼내기
url = 'https://www.wadiz.kr/web/campaign/detail/140132'
webpage = urlopen(url)
source = BeautifulSoup(webpage, 'html.parser')
# request.get으로 쓰는 걸 더 권장 (한글도 가능)
# web = requests.get(url).content
# source = BeautifulSoup(web, 'html.parser')
target = source.find_all('button', {'class':'rightinfo-reward-list'})
4) 마감 상품 재고 확인
- len으로 접근
target = source.find_all('button', {'class':'rightinfo-reward-list'})
# item == "button" tag
for item in target:
if '179,000' in item.find('dt').get_text().strip():
# print(item.find('dt').get_text().strip())
if '블루' in item.find('p').get_text().strip():
# print(item.find('p').get_text().strip())
# 어떻게 soldout이 풀리는지 아는가? 우리는 len을 접근
if len(item.attrs['class']) == 2:
# 메일 발송
5) 최종 마감 상품 재고 확인 및 메일 발송하기
check_status = 1
while check_status: # check_status == 1
webpage = urlopen(url)
source = BeautifulSoup(webpage, 'html.parser')
target = source.find_all('button', {'class':'rightinfo-reward-list'})
# item == "button" tag
for item in target:
if '179,000' in item.find('dt').get_text().strip():
# print(item.find('dt').get_text().strip())
if '블루' in item.find('p').get_text().strip():
# print(item.find('p').get_text().strip())
if len(item.attrs['class']) == 2:
# print(len(item.attrs['class']))
sendMail('~~~@gmail.com', # Sender
'~~~@gmail.com', # Receiver
'99800 Available \n https://goo.gl/wG8v1F%27') # 본문
check_status = 0
time.sleep(10)
확인을 위해 len(item.attrs['class'])==3 으로 수정하고 메일 발송을 확인해보았다.
'멋쟁이 사자처럼 AI SCHOOL 5기 > Today I Learned' 카테고리의 다른 글
[3주차 총정리] 웹스크래핑, table 태그 설명 (네이버 상승주) (0) | 2022.03.29 |
---|---|
[3주차 총정리] Post Request 기반의 웹사이트 크롤링 (서울상권분석서비스) (0) | 2022.03.29 |
[3주차 총정리] Selenium으로 브라우저 제어 자동화하기(2) _ 인터파크 투어 크롤링 (0) | 2022.03.29 |
[3주차 총정리] Selenium으로 브라우저 제어 자동화하기 (0) | 2022.03.29 |
[3주차 총정리] Okt 텍스트 분석 후에 Word Cloud 제작하기 (0) | 2022.03.29 |