scrapy之文件自动打包并发送邮件

send_all_mail.py

def send_email(subject="邮件标题", content="看看能不能发送成功", receive_email="xxxxxxx",
               filepath=[]):  # filepath是附件文件,如果有附件,则需要给下面设置
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    sender = 'xxxxxxxx'  # 发送方邮箱
    passwd = 'xxxxxxxx'  # 填入发送方邮箱的授权码
    receivers = receive_email  # 收件人邮箱
    msgRoot = MIMEMultipart()
    msgRoot['Subject'] = subject
    msgRoot['From'] = sender
    if len(receivers) > 1:
        msgRoot['To'] = ','.join(receivers)  # 群发邮件
    else:
        msgRoot['To'] = receivers[0]
    part = MIMEText(content, _charset="utf-8")
    msgRoot.attach(part)
    for path in filepath:
        part = MIMEApplication(open(path, 'rb').read())
        part.add_header('Content-Disposition', 'attachment', filename=path)
        msgRoot.attach(part)
    s = smtplib.SMTP('smtp.163.com', 25)
    try:
        s.login(sender, passwd)
        s.sendmail(sender, receivers, msgRoot.as_string())
        print("邮件发送成功")
    except smtplib.SMTPException as e:
        print("Error, 发送失败")
    finally:
        s.quit()


# 当.py文件被直接运行时,if _name_ == '_main_'之下的代码块将被运行;当.py文件以模块形式被导入时,if _name_ == '_main_'之下的代码块不被运行。
print(__name__)
if __name__ == '__main__':
    print(__name__, 11111111111)
    subject = "邮件标题"
    content = "看看能不能发送成功"
    receive_email = ["xxxxxx"]
    send_email(subject, content, receive_email)

pipelines.py

import os
import zipfile
from sent_all_mail import send_email
from scrapy.pipelines.files import FilesPipeline

class FilesPipeline(FilesPipeline):
    count = 0
    novel_zip_list = []
    
	# 获取item数据
    def get_media_requests(self, item, info):
        return [Request(x, meta={"item": item}) for x in item.get(self.files_urls_field, [])]

	# 修改下载路径
	 def file_path(self, request, response=None, info=None):
        item = request.meta['item']
        return f"小说/{item['novel_category']}/{item['novel_title']}.txt"

	# 获取下载路径
    def item_completed(self, results, item, info):
        item['novel_download_path'] = results[0][1]['path']
        print("下载完成")

        def zipDir(filepath, zippath):
            zip = zipfile.ZipFile(zippath, "w", zipfile.ZIP_DEFLATED)
            zip.write(filepath, filepath.split("/")[-1])
            zip.close()

        curPath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
        # zipDir('C://1.jpg','C://1.zip')
        fpath = curPath + "/80小说/" + item['novel_download_path']
        zipDir(fpath, fpath.replace(".txt", ".zip"))
        FilesPipeline.count += 1
        self.novel_zip_list.append(fpath.replace(".txt", ".zip"))
        if FilesPipeline.count % 5 == 0:
            send_email(filepath=self.novel_zip_list)
            self.novel_zip_list.clear()
            # self.novel_zip_list = []
            print("可以发邮件了")
        return item

   

猜你喜欢

转载自blog.csdn.net/qq_41150890/article/details/100574102