Python tracks novel updates and Alerts by email

Source: Internet
Author: User
Tags xpath

#!/usr/bin/env python#Coding:utf-8 fromUrllibImportrequest, ParseImportOSImport TimeImportRandom fromUrllib.errorImportUrlerror, Httperror fromEmailImportencoders fromEmail.headerImportHeader fromEmail.mime.textImportMimetext fromEmail.utilsImportparseaddr, Formataddr fromEmail.mime.multipartImportMimemultipartImportSmtplib fromlxmlImportetreeclassSpider:def __init__(self): Self.old_title_qidian=list () Self.old_title_zongheng=list ()defcheck_update (self):#read the latest chapter in the last record from a filef = open ("Qidian.txt",'R') Self.old_title_qidian=[] Link_qidian=[] n=0 forLineinchOpen'Qidian.txt'):            if(n% 2 = = 1): Line=f.readline () Line=Line.strip () self.old_title_qidian.append (line)Else: Line=f.readline () Line=Line.strip () link_qidian.append (line) n= n + 1F.close () F= Open ("Zongheng.txt",'R') Self.old_title_zongheng=[] Link_zongheng=[] n=0 forLineinchOpen'Zongheng.txt'):            if(n% 2 = = 1): Line=f.readline () Line=Line.strip () self.old_title_zongheng.append (line)Else: Line=f.readline () Line=Line.strip () link_zongheng.append (line) n= n + 1f.close ()Print(Self.old_title_qidian)Print(Link_qidian)Print(Self.old_title_zongheng)#Detect Updatesi =0 forKinchlink_qidian:self.update_2 (i, k) I= i + 1I=0 forKinchLink_zongheng:self.update_3 (i, k) I= i + 1#save a new chapter namef = open ("Qidian.txt","W") Length=Len (Link_qidian) forIinchRange (length): F.write (Link_qidian[i]+'\ n') F.write (Self.old_title_qidian[i]+'\ n') F.close () F= Open ("Zongheng.txt","W") Length=Len (Link_zongheng) forIinchRange (length): F.write (Link_zongheng[i]+'\ n') F.write (Self.old_title_zongheng[i]+'\ n') F.close ()#Get HTML    defopen_url (self, link): Wanted_page=Link req=request. Request (wanted_page) Req.add_header ('user-agent','mozilla/5.0 (Windows NT 10.0; Win64; x64) applewebkit/537.36'                                   '(khtml, like Gecko) chrome/51.0.2704.79 safari/537.36 edge/14.14393') Response=Request.urlopen (req) HTML= Response.read (). Decode ('Utf-8')        returnHTML#check whether to update the Chinese network for starting point    defupdate_2 (self, I, link): HTML=self.open_url (link) tree=etree. HTML (HTML) node= Tree.xpath (U"/html/body/div[2]/div[6]/div[4]/div[1]/div[1]/div[2]/ul/li[3]/div/p[1]/a/@title")        Print(node[0])if(node[0]! =Self.old_title_qidian[i]): Self.old_title_qidian[i]=Node[0] New_email=SendEmail (node[0]) new_email.send (link)Else:            Print(0)#Check to see if the update is for the Chinese network    defupdate_3 (self, I, link): HTML=self.open_url (link) tree=etree. HTML (HTML) node= Tree.xpath (U"/html/body/div[6]/div[1]/div/div[3]/a/text ()") node[0]= Node[0].strip ()#the header of the starting point reads a newline character more, to remove        Print(node[0])if(node[0]! =Self.old_title_zongheng[i]): Self.old_title_zongheng[i]=Node[0] New_email=SendEmail (node[0]) new_email.send (link)Else:            Print(0)classSendEmail:def __init__(self, title): Self.title=titledefSend (self, link):#XXXXXXXXX represents the Mailbox service authorization CodeData_1 = ['[email protected]','xxxxxxxxxx','[email protected]','smtp.qq.com'] From_addr=data_1[0] Password= Data_1[1] To_addr= Data_1[2] Smtp_server= Data_1[3] MSG= Mimemultipart ('Alternative') msg[' from'] =from_addr msg[' to'] =to_addr msg['Subject'] = R'Comrade, the novel updated!!! 'HTML=""""""+ link +"""">"""+ Self.title +"""<a><br> """part1= Mimetext (HTML,'HTML') Msg.attach (part1)Try: Server= Smtplib. Smtp_ssl (Smtp_server, 465) Server.set_debuglevel (1) server.login (from_addr, password) server.sendmail (from_addr, To_addr, msg.as_string ()) Print('Success')        exceptserver. Smtpexception as E:Print("failed")        finally: Server.quit ()if __name__=='__main__': Update_spider=Spider () while(1):        #record the current timef = open ("Xs_log.txt","a") Now_time= Time.strftime ('%y-%m-%d%H:%m:%s', Time.localtime (Time.time ())) F.write (Now_time+'\ n') F.close () update_spider.check_update ()#Check for updates every 10 minutesTime.sleep (600)    

The first time you write, the latest chapters are used in regular, then changed, XPath.

If you do not want to use the file to save the link information and the latest chapter name, you can directly write in the code to save with a list.

Effect:

Python tracks novel updates and Alerts by email

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.