爬虫 监控 自动 匹配 抓取 卡饭 论坛 帖子 发邮件 python

值得注意的是MIMEText接收参数编码问题,灵活运用.decode().encode()

contentpre = (listitem1[91:-4]).decode('gbk').encode('utf-8')
content = []
content.append(contentpre)
contentstr = "".join(content)
msg = MIMEText(contentstr)

下面是正文,大部分内容是之前的爬虫里的,冗余代码有空再删

# -*- coding: utf-8 -*-
import urllib2
import requests
import http.cookiejar
import re
import smtplib
import time
import sys
from email.mime.text import MIMEText

_user = "xxxxxxxxx@qq.com"
_pwd = "xxxxxxxxxx"
_to = "xxxxxxxxxxx@360.cn"
#病毒样本区,增加访问报错重试
req1 = urllib2.Request("http://bbs.kafan.cn/forum-111-1.html")
retries1=15
class Urlrequest:
    def get(self, req, retries):
        try:
            response = urllib2.urlopen(req)
            bufferread = response.read()
        except Exception, what:
            #print what, req
            if retries > 0:
                return self.get(req, retries - 1)
            else:
                print 'GET Failed', req
        return bufferread
buff = Urlrequest()
buffer = buff.get(req1, retries1)

#<a href="http://bbs.kafan.cn/thread-2083448-1-1.html" onclick="atarget(this)" class="s xst">音乐弹窗(纯娱乐)</a>
#re.compile(r'http://bbs.360.cn/thread-.+-1-1.html" \s?target="_blank" class="s xst".*\s*</a>')

getarticlelist = re.compile(r'<a href="http://bbs.kafan.cn/thread-.+-1-1.html" \s?onclick=.*\s*</a>')
pagemsg = re.findall(getarticlelist,buffer)
allurllist = []
for eveurllist in pagemsg:
    allurllist.append(eveurllist[9:52])
print len(allurllist)

#开始循环检验了,时间差大点,让卡饭服务器喘口气
while True:
    time.sleep(10)
    buff1 = Urlrequest()
    buffer1 = buff1.get(req1, retries1)
    getarticlelist1 = re.compile(r'<a href="http://bbs.kafan.cn/thread-.+-1-1.html" \s?onclick=.*\s*</a>')
    pagemsg1 = re.findall(getarticlelist1, buffer1)
    for listitem1 in pagemsg1:
        if listitem1[9:52] in allurllist:
            pass
        else:
            allurllist.append(listitem1[9:52])
            contentpre = (listitem1[91:-4]).decode('gbk').encode('utf-8')
            content = []
            content.append(contentpre)
            contentstr = "".join(content)
            msg = MIMEText(contentstr)
            # 链接
            msg["Subject"] = listitem1[9:52]
            msg["From"] = _user
            msg["To"] = _to
            try:
                s = smtplib.SMTP_SSL("smtp.qq.com", 465)
                s.login(_user, _pwd)
                s.sendmail(_user, _to, msg.as_string())
                s.quit()
                print "Send Email Success!"
            except smtplib.SMTPException, e:
                print "Send Email Falied,%s" % e
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值