公告通知:
咨询电话:400-686-1060

您当前的位置:首页>>帮助中心>>IT技术交流论坛 IT技术交流论坛

利用python 删除腾讯企业邮箱中多余的邮件
发布时间:2019-06-25 丨 阅读次数:90

一、后台

小编公司所有的是腾讯企业邮箱,由于公司自动化 以是老是主动给邮箱发送不少过剩的邮件,但是有必需需求接受,因而小编写了一个剧本,准时清算这个邮件。

二、思绪

起首,登录肯定是破不了的,因而小编接纳的是cookie坚持登录。

三、代码

# author:Administrator

# datetime:2019/4/10 12:31

# project_name:python_code_warehouse

# file_name:del_tengxun_email

# email:1163739580@qq.com 删除腾讯企业邮箱邮件 只需要替代self.cookie 以及sid即可,此外需求配置self.del_emails_user

import requests

from lxml import etree

import time

import logging

from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

LOGGER = logging.getLogger(“运转日记:“)

LOGGER.setLevel(logging.DEBUG)

HANDLER = logging.FileHandler(“log.txt“, encoding=‘utf-8‘)

HANDLER.setLevel(logging.DEBUG)

CONHANDLER = logging.StreamHandler()

CONHANDLER.setLevel(logging.INFO)

FORMATER = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)

LOGGER.addHandler(HANDLER)

LOGGER.addHandler(CONHANDLER)

HANDLER.setFormatter(FORMATER)

CONHANDLER.setFormatter(FORMATER)

class DelTengXunEmail(object):

    def __init__(self):

        self.host = “https://exmail.qq.com“

        self.header = {

            ‘User-Agent‘: ‘Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/73.0.3683.86Safari/537.36‘}

        self.cookie = {‘你的cookie‘: ‘xxx‘}

        self.sid = “你的sid“

        “““需求删除的邮件发送者: 假如删除全数 ,设置成空元组即可“““

        self.del_emails_user = ()

    def del_tengxun_email(self):

        cnt = 0  # 轮回次数变量

        while True:

            for i in range(100):

                LOGGER.info(“以后正在删除%d页 邮件“ % (i + 1))

                params = {‘filetype‘: ‘‘, ‘showattachtag‘: ‘‘, ‘listmode‘: ‘‘, ‘flag‘: ‘‘, ‘fun‘: ‘‘, ‘category‘: ‘‘,

                          ‘searchmode‘: ‘‘, ‘stype‘: ‘‘, ‘grpid‘: ‘‘, ‘AddrID‘: ‘‘, ‘ftype‘: ‘‘, ‘page‘: i,

                          ‘folderid‘: ‘1‘, ‘sid‘: self.sid, ‘s‘: ‘inbox‘}

                str_html = requests.get(url=“https://exmail.qq.com/cgi-bin/mail_list“, params=params, verify=False,

                                        headers=self.header, cookies=self.cookie).text

                email_info = self.get_email_info_by_strhtml(str_html)

                self.del_email_by_emails_info_list(*email_info)

                if i == 99:

                    cnt = cnt + 1

                    LOGGER.info(“第%s次轮回100页 完结“ % cnt)

                if cnt == 50:  # 五次轮回遍历后退出

                    LOGGER.info(“遍历50次 100页轮回实现  步伐完结“)

                    exit()

    def get_email_info_by_strhtml(self, str_html):

        “““

        :param str_html: string范例的html

        :return: 前往email信息 [{email_id:‘邮件id‘,email_in_user:‘邮件发送者‘,‘email_name‘:‘邮件题目‘,‘email_time‘:‘邮件收到日期‘},{}]

        “““

        email_info = []

        email_dict = {}

        email_id = etree.HTML(str_html).xpath(

            ‘//table[@cellspacing=“0“]/tr/td[@class=“cx“]/input/@value‘)

        email_in_user = etree.HTML(str_html).xpath(

            ‘//table[@cellspacing=“0“]/tr/td[@class=“cx“]/input/@fa‘)

        email_name = etree.HTML(str_html).xpath(

            ‘//table[@cellspacing=“0“]/tr/td[@class=“gt tf“]/div[1]/u/text()‘)

        email_time = etree.HTML(str_html).xpath(

            ‘//table[@cellspacing=“0“]/tr/td[@class=“dt“]/div/text()‘)

        email_time = self.remove_blank_space(*email_time)

        for i in range(len(email_id)):

            email_dict[“email_id“] = email_id[i]

            email_dict[“email_in_user“] = email_in_user[i]

            email_dict[“email_name“] = email_name[i]

            email_dict[“email_time“] = email_time[i]

            email_info.append(email_dict)

            email_dict = {}

        if len(email_info) == 0 and “收件箱“ not in str(str_html):

            LOGGER.error(“cookie和sid生效,请更新!!“)

            exit()

        return email_info

    @staticmethod

    def remove_blank_space(*ak):

        “““将序列内里的每一个值,去除空格

        :param ak: 接收的序列

        :return:序列

        “““

        sequence = []

        for a in ak:

            sequence.append(““.join(str(a).split()))

        return sequence

    def del_email_by_emails_info_list(self, *emails_info_list):

        “““删除邮件

        :param emails_info_list: emails_info_list信息列表

        “““

        del_emails_ids = ““

        for emails in emails_info_list:

            if len(self.del_emails_user):

                if emails.get(“email_in_user“) in self.del_emails_user:

                    LOGGER.info(“收集到邮件(详情信息为{})“.format(emails))

                    del_emails_ids = del_emails_ids + “%s&mailid=“ % (emails.get(“email_id“))

            else:

                LOGGER.info(“收集到邮件(详情信息为{})“.format(emails))

                del_emails_ids = del_emails_ids + “%s&mailid=“ % (emails.get(“email_id“))

        if del_emails_ids == ““:

            LOGGER.info以后页数吻合前提邮件....“)

        else:

            self.del_email_by_email_id(del_emails_ids[:-8])

    def del_email_by_email_id(self, email_id):

        “““删除邮件

        :param email_id: 邮件id

        “““

        url = self.host + “/cgi-bin/mail_mgr?sid=%s“ % self.sid

        body = {‘mailaction‘: ‘mail_del‘, ‘location‘: ‘mail_list‘, ‘mailid‘: email_id,

                # email_id为多个mailid = ZC0714-VV3rH0RJdhxHZU8VQNECC8d&mailid=ZC0514-srpFfiXe8JrACiB6VNMdA8d&mailid=ZC0714-Xlb5_qV0LUcdeFIIZODKG8d&mailid=ZC0714-9Py_B1yNjOa8dlwGVOapE8d

                ‘t‘: ‘mail_mgr2‘, ‘resp_charset‘: ‘UTF8‘, ‘ef‘: ‘js‘, ‘sid‘: self.sid}

        str_body = self.dict_to_str(**body)

        flag = requests.post(url=url, cookies=self.cookie, headers=self.header, verify=False, data=str_body).text

        time.sleep(2)

        if “mail_del successful“ in str(flag):

            LOGGER.info总计 %d 邮件删除胜利!!“ % str_body.count(“mailid“))

        else:

            LOGGER.error(“邮件删除失败! {}“.format(flag))

            exit()

    @staticmethod

    def dict_to_str(**kw):

        “““字典转换成字符串 key1=vlaue1&key2=value2

        :param kw: 字典

        “““

        str_dict = ““

        for key in kw:

            str_dict = str_dict + “%s=%s&“ % (key, kw.get(key))

        return str_dict[:-1]

if __name__ == “__main__“:

    xiejiangpeng = DelTengXunEmail()

    xiejiangpeng.del_tengxun_email()




收缩
  • 在线咨询
  • 电话咨询

  • 400-686-1060
  • 13636809210
  • 使用微信扫码