본문으로 바로가기

이를 하기 위해서는 구글 내 계정에서 앱 비밀번호를 설정해주어야 합니다.

- 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 으로 수정하고 메일 발송을 확인해보았다.