python实现扫描论坛回帖,自动发附件(应对求种之类的)
背景:
笔者因为需要在 www.kindle114.com 上分享一些图书,鉴于各种网盘神马的,都会把涉及版权的分享给失效了,总是换分享连接很麻烦,所以不得已只能通过邮件来传播知识,这就需要每天去看回帖的邮箱,一一发送,甚是麻烦。用一个python的脚本去扫描这些页面,把邮箱地址抓出来后自动发送才是懒人应该做的事情。
实现过程:
需要用crontab来实现每天定时执行这个脚本。
脚本一共分三部分:
1. 遍历指定帖子的所有回帖页,抓取Email格式的字符串,遇到某些情况就终止扫描
2. 比对过去发送过的列表,如果是新出现的则加入发送列表中
3. 将发送列表用python的email功能发送出去,并且带上附件
可以改进的地方:
1. 记录下每次扫描的终点,下次从这个位置扫描,避免重复扫描的浪费
2. 将URL和附件做成字典,可以灵活发送
3. 做成一个在线系统来维护这个数据和列表
注意点:
1. python的sendmail,发送列表要用list类型的
2. 如果是QQ邮箱,要把邮箱的独立密码去掉(我也不知道如果不去掉应该怎么去验证),否则会出现验证失败的错误
3. 可能有人会因为隐私原因,把邮箱地址写成怪怪的样子,那我就没辙了...
4 本文只是抛砖引玉,论坛形式千变万化,需要灵活修改脚本来实现网页抓取
5. 鉴于现在出版物粗糙的太多了,所以想先预览一下合情合理。如果觉得电子书或者视频有价值,请去电影院看电影或购买正版书籍,写这个短短文章都需要花费时间和精力,何况是写书和拍片,人家就是靠这个谋生。
#!/usr/bin/python import sys, urllib, re from email.Header import Header from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart import 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[‘subject‘] = 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(‘1234567‘,‘xxxxxxxxxxxx‘) error=server.sendmail(msg[‘from‘],toWho,msg.as_string()) server.close print error dicSave = {} for line in open("log"): dicSave[line.rstrip()] = 1 dic = {} preDic = {} ## grep Email format string pattern = 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 page for 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 run file_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,‘[email protected]‘,‘Redis_shejiyushixian.mobi‘,‘What are you interested in ?‘) print "Deliver is completed...\n"; else: print "Mail List is empty\n";
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。