Monitor novel update status sent to mailbox (crawler and mail

Keywords: Python Mobile SSL Windows

Originally published at https://blog.csdn.net/qq_21484935/article/details/103461778

Idea: request the url of the novel and analyze the content to find the span tag with the update time. Then configure the mailbox to send the content as.

My choice is Netease's 126 email. I log in to my account on the official website. In the settings, open "POP3/SMTP/IMAP" (you need to send a verification message from your mobile phone here)
After the setting is successful, as shown in the figure:

The port information is as follows:

The next step is very simple, SMTP operation of python (Baidu will not be invited
No more nonsense, just go to the code

import logging
import smtplib
from email.mime.text import MIMEText
import requests
from bs4 import BeautifulSoup



server = 'smtp.126.com' 
sender = 'xxxx@126.com' #Send mailbox
mailpass = 'xxxxx' #Client authorization code
receivers =['xxxxx@qq.com'] #Receiving mailbox

def getUrl(url , header):
    req = requests.get(url , header)
    bs = BeautifulSoup(req.text , 'html5lib')
    time = bs.select('body > div.wrap.cf > div.main > div.chapterlist > div.chapterlist_box > div > div > span')[0]
    return time.string  #Return to update time

def send(text):
    snd_msg = MIMEText(text,'plain' ,'utf-8')
    snd_msg['From'] = "{}".format(sender)
    snd_msg['To'] = ",".join(receivers)
    snd_msg['Subject'] = '<Ghost knife>Update information'
    try:
        smtp = smtplib.SMTP_SSL(server, 465) #ssl protocol port 465
        smtp.login(sender , mailpass) #Sender email client authorization code
        smtp.sendmail(sender , receivers , snd_msg.as_string())
        print('Send successfully!')
    except smtplib.SMTPException:
        logging.error('failed to send email!')


def init(url):
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
    }
    try:
        text = getUrl(url , header)
        return text
    except Exception as e:
        print(e)


if __name__ == "__main__":
    url = 'http://www.u17.com/comic/68471.html'
    text = init(url)
    send(text)

The next steps are just a few more. You can set when to apply, mount to the server, check whether the update is not available every other period of time, and then send it to the mailbox. Isn't it beautiful. (this cartoon hasn't been seen. Many small partners can tuck up.

Posted by PABobo on Wed, 11 Dec 2019 09:50:06 -0800