Python scans Forum replies and automatically sends attachments (such as requests)
Background: The author needs to share some books on www.kindle114.com. In view of the various online storage devices, copyright-related sharing will become invalid and it is always troublesome to change the sharing connection, therefore, it is imperative that you only use emails to disseminate knowledge. Therefore, you need to go to the reply email every day and send them one by one, which is very troublesome. Use a python script to scan these pages, and automatically send the email address after it is captured. Implementation process: You need to use crontab to implement daily scheduled execution of this script. The script consists of three parts: 1. traverse all reply pages of a specified post, capture strings in the Email format, and terminate scanning in some cases. compare the list sent in the past. If it is a new one, add it to the sending list. 3. you can use the python email function to send the sending list and add the attachment to the list. record the end point of each scan and scan from this position next time to avoid repeated scans. make the URL and attachments into dictionaries and send them flexibly. make an online system to maintain this data and list. Note: 1. sendmail in python. The sending list must be of list type 2. if it is a QQ mailbox, remove the independent password of the mailbox (I do not know how to verify it if it is not removed), otherwise there will be a verification failure error 3. someone may write the email address as a strange look for privacy reasons, so I will be confused... 4. This article is just a reference, and the Forum form is ever-changing. You need to modify your scripts flexibly to capture webpages. 5. since there are too many publications, it is reasonable to preview them first.. If you think that e-books or videos are valuable, please go to the cinema to watch movies or purchase genuine books. It takes time and effort to write this short article, not to mention writing books and making movies. Copy Code #! /Usr/bin/python import sys, urllib, refrom email. header import Headerfrom email. MIMEText import MIMETextfrom email. MIMEMultipart import MIMEMultipartimport smtplib, datetime def sendMail (toWho, fromWho, bookName, text): msg = MIMEMultipart () # fill your BT torrent or eBook... att = MIMEText (open (bookName, 'rb '). read (), 'base64', 'gb2312') att ["Content-Type"] = 'application/octet-stream' att ["Content -Disposition "] = 'attachment; filename =" Redis_shejiyushixian.mobi "'msg. attach (att) msg ['from'] = fromWho msg ['subobject'] = Header (bookName + '(' + str (datetime. date. today () + ')', 'gb2312 ') msg. attach (MIMEText (text) server = smtplib. SMTP ('smtp .qq.com ') # fill your QQ number and password server. login ('20140901', 'xxxxxxxxxxxxxx') error = server. sendmail (msg ['from'], toWho, msg. as_string () server. close p Rint error dicSave ={} for line in open ("log"): dicSave [line. rstrip ()] = 1 dic ={} preDic ={}## grep Email format stringpattern = re. compile (R '[_. 0-9a-z-] + @(?: [0-9a-z] [0-9a-z-] + .) + [a-z] {2, 3} \ B ') # please enhance this script to mark stop page as next start pagefor x in range (1,100 ): # fill forum url which need scan url = "http://www.kindle114.com/thread-4567-%d-1.html" % x print "% s" % url wp = urllib. urlopen (url) content = wp. read () # get mail list m = pattern. findall (content) stopFlag = 1 for I in m: # if page is duplicate means scan is wasting time And need stop if I not in preDic: stopFlag = 0 # only new mail address is needed to deliver if I not in dicSave: dic [I] = 1 if stopFlag = 1: print "Scan Page Over % d \ n" % x break preDic = m mailList = [] # write deliver mail address to log to avoid duplicate deliver in next runfile_object = open ("log ", 'A + ') for k, v in dic. items (): print k mailList. append (k) file_object.write (k + "\ n") file_object. Close () if mailList: print "Delivering... \ n"; sendMail (mailList, '2017 @ qq.com ', 'redis _ shejiyushixian.mobi', 'What are you interested in? ') Print "Deliver is completed... \ n"; else: print "Mail List is empty \ n ";