python实现扫描论坛回帖,自动发附件(应对求种之类的)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

背景:

笔者因为需要在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/pythonimportsys, urllib, refromemail.HeaderimportHeaderfromemail.MIMETextimportMIMETextfromemail.MIMEMultipartimportMIMEMultipartimportsmtplib, datetimedefsendMail(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 passWordserver.login('1234567','xxxxxxxxxxxx')

error=server.sendmail(msg['from'],toWho,msg.as_string())

server.closePRinterror

dicSave={}forlineinopen("log"):

dicSave[line.rstrip()]= 1dic={}

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 pageforxinrange(1,100):## fill forum url which need scanurl="http://www.kindle114.com/thread-4567-%d-1.html"%xprint"%s"%url

wp=urllib.urlopen(url)

content=wp.read()## get mail listm =pattern.findall(content)

stopFlag= 1foriinm:## if page is duplicate means scan is wasting time and need stopifinotinpreDic:

stopFlag=0## only new mail address is needed to deliverifinotindicSave:

dic[i]= 1ifstopFlag == 1:print"Scan Page Over %d\n"%x

breakpreDic=m

mailList=[]## write deliver mail address to log to avoid duplicate deliver in next runfile_object = open("log",'a+')fork,vindic.items():printk

mailList.append(k)

file_object.write(k+"\n")

file_object.close()ifmailList:print"Delivering...\n";

sendMail(mailList,'77167680@qq.com','Redis_shejiyushixian.mobi','What are you interested in ?')print"Deliver is completed...\n";else:print"Mail List is empty\n";

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航