admin管理员组

文章数量:1559070

一、背景

小编公司所有的是腾讯企业邮箱,因为公司自动化 所以总是自动给邮箱发送很多多余的邮件,但是有必须需要接收,于是小编写了一个脚本,定时清理这个邮件。

二、思路

首先,登录肯定是破不了的,于是小编采用的是cookie保持登录。

三、代码

# author:administrator
# datetime:2019/4/10 12:31
# project_name:python_code_warehouse
# file_name:del_tengxun_email
# email:1163739580@qq 删除腾讯企业邮箱邮件 只需要替换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"
        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/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[@]/input/@value')
        email_in_user = etree.html(str_html).xpath(
            '//table[@cellspacing="0"]/tr/td[@]/input/@fa')
        email_name = etree.html(str_html).xpath(
            '//table[@cellspacing="0"]/tr/td[@]/d

本文标签: 腾讯企业邮箱邮件python