值得注意的是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 urllib2import requestsimport http.cookiejarimport reimport smtplibimport timeimport sysfrom email.mime.text import MIMEText_user = "xxxxxxxxx@"_pwd = "xxxxxxxxxx"_to = "xxxxxxxxxxx@"#病毒样本区,增加访问报错重试req1 = urllib2.Request("/forum-111-1.html")retries1=15class Urlrequest:def get(self, req, retries):try:response = urllib2.urlopen(req)bufferread = response.read()except Exception, what:#print what, reqif retries > 0:return self.get(req, retries - 1)else:print 'GET Failed', reqreturn bufferreadbuff = Urlrequest()buffer = buff.get(req1, retries1)#<a href="/thread-2083448-1-1.html" onclick="atarget(this)" class="s xst">音乐弹窗(纯娱乐)</a>#pile(r'/thread-.+-1-1.html" \s?target="_blank" class="s xst".*\s*</a>')getarticlelist = pile(r'<a href="/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 = pile(r'<a href="/thread-.+-1-1.html" \s?onclick=.*\s*</a>')pagemsg1 = re.findall(getarticlelist1, buffer1)for listitem1 in pagemsg1:if listitem1[9:52] in allurllist:passelse: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"] = _usermsg["To"] = _totry:s = smtplib.SMTP_SSL("", 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
如果觉得《爬虫 监控 自动 匹配 抓取 卡饭 论坛 帖子 发邮件 python》对你有帮助,请点赞、收藏,并留下你的观点哦!