Use Python to realize website content monitoring and email reminder

Table of contents

1. Open POP3/SMTP service

2. Use the SMTP library to send emails

3. Use the requests library to monitor webpage keywords

4. Frequently asked questions


1. Open POP3/SMTP service

Take QQ mailbox as an example: enter mailbox->settings->account->enable POP3/SMTP and other services->obtain authorization code

① First enter the QQ mailbox and click Settings

② Click the account in the mailbox setting

 ③ Scroll down to find the POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV service and open it, follow the steps to obtain the verification code and obtain the authorization code after verification

 

 ④Record the authorization code, which needs to be configured in the code later

2. Use the SMTP library to send emails

 This article only shows the code that can realize related functions. For more SMTP libraries , please refer to the following information:

①Chapter 11: Python automation - SMTP library for mail sending - Programmer Sought

 ②Use Python to send mail automatically

2.1 smtp_obj

smtp_obj = smtplib.SMTP( [host [, port [, local_hostname]]] )
参数:

host: SMTP server host
port: If the host parameter is provided, specify the port number used by the SMTP service
local_hostname: When SMTP is on the local machine, only need to specify the server address as localhost 

2.2 MIMEText (implementing support for emails in HTML format)

(1) Email content
text = "Pass"

msg = MIMEText(text,'plain', 'utf-8') 

(2) Mail subject
msg['Subject'] = 'title'

(3) sender
msg['From'] = sender

2.3 SMTP.sendmail

The SMTP object uses the sendmail method to send mail

SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
参数:

from_addr: sender address.
to_addrs: target mailbox
msg: send message

import smtplib
from email.mime.text import MIMEText
from email.header import Header

def send_email():
    smtp_obj = smtplib.SMTP('smtp.qq.com',587)
    smtp_obj.login('[email protected]','对应授权码')

    mail_text = '邮件正文'
    msg_body = MIMEText(mail_text,'plain','utf-8')
    msg_body['From'] = Header('zsl<[email protected]>')  #发送人->参考RFC2047, RFC822协议
    msg_body['Subject'] = Header('邮件标题','utf-8')  #内容主题

    smtp_obj.sendmail('[email protected]','[email protected]',msg_body.as_string())

send_email()

operation result:

3. Use the requests library to monitor webpage keywords

Taking the notification announcement of the School of Mechanical and Electrical Engineering of Beijing Institute of Technology as an example, if you want to monitor the "School of Mechanical and Electrical Engineering's 2023 Ph. The key word judges whether the relevant content appears in the webpage, and when the target field appears, use break to end the loop.

The complete code is as follows :

import smtplib
from email.mime.text import MIMEText
from email.header import Header
import requests
from bs4 import BeautifulSoup
import time

def send_email():
    smtp_obj = smtplib.SMTP('smtp.qq.com',587)

    smtp_obj.login('[email protected]','授权码')

    mail_text = 'Pass'
    msg_body = MIMEText(mail_text,'plain','utf-8')
    msg_body['From'] = Header('zsl<[email protected]>') 
    msg_body['Subject'] = Header('机电学院2023年博士研究生招生拟录取','utf-8') 

    smtp_obj.sendmail('[email protected]','[email protected]',msg_body.as_string())


while True:
    try:
        url = "https://smen.bit.edu.cn/tzgg1/index.htm"
        res = requests.get(url)
        res.encoding = res.apparent_encoding

        if "机电学院2023年博士研究生招生拟录取" in res.text  :
            print("test")
            send_email()
            break

        time.sleep(30)
    
    except Exception as e:
        print(e)

4. Frequently asked questions

Notice:

① You need to enter the authorization code corresponding to your email ! ! !

②Error : header is missing or invalid. Please follow RFC5322, RFC2047, RFC822 standard pro-> Solution : From( msg_body['From'] ) legitimacy check ( help system )->Designation problem, sender's Chinese name coding problem

③Error : Connection unexpectedly closed-> Solution : smtp_obj = smtplib.SMTP('smtp.qq.com',587) Set the sending server port number to 465 or 587

Guess you like

Origin blog.csdn.net/zsllsz2022/article/details/131606189