自动化统计邮箱邮件

#encode=utf-8
 
class Practice(object):
    print u"这个方法的功能仅限于统计邮件"
    def __init__(self,url,driver):
        self.driver=driver
        self.url=url
 
    def brower_content(self):
self.driver.get(self.url)
        mail_content=[]
        try:
            self.driver.switch_to_frame("login_frame")
            self.driver.find_element_by_id("img_out_1300994936").click()
            time.sleep(5)
            self.driver.switch_to.default_content()
            self.driver.find_element_by_id("folder_8").click()
            time.sleep(5)
            self.driver.switch_to_frame("mainFrame")
            self.driver.find_element_by_id("ckb_selectAll").click()
            time.sleep(1)
            self.driver.find_element_by_id("setAllReaded").click()
            time.sleep(1)
            result=self.driver.find_elements_by_xpath(".//table[@class='i  M']")
        except Exception:
            self.driver.switch_to_frame("login_frame")
            self.driver.find_element_by_id("switcher_plogin").click()
            self.driver.find_element_by_id("u").send_keys("1300994936@qq.com")
            self.driver.find_element_by_id("").send_keys(password)
            self.driver.find_element_by_id("login_button").click()
            time.sleep(5)
            self.driver.switch_to.default_content()
            self.driver.find_element_by_id("folder_8").click()
            time.sleep(5)
            self.driver.switch_to_frame("mainFrame")
            self.driver.find_element_by_id("ckb_selectAll").click()
            time.sleep(1)
            self.driver.find_element_by_id("setAllReaded").click()
            time.sleep(1)
            result=self.driver.find_elements_by_xpath(".//table[@class='i  M']")
        else:
            print u"获取邮箱内容失败"
        for i in result:
            if i.find_element_by_xpath(".//td[@class='tl_g tf']").text.decode("utf-8")[:-1]==u"光荣之路涨薪学习2群":
                checkout=i.find_element_by_xpath(".//td[@class='dt']//div").text
                print checkout[3:5],i.find_element_by_xpath(".//td[@class='gt tf']//u").text
                if re.match(u"今天",checkout) and int(checkout[3:5])<9:
                    mail_content.append(i.find_element_by_xpath(".//td[@class='gt tf']//u").text)
                    print i.find_element_by_xpath(".//td[@class='gt tf']//u").text
                elif re.match(u"昨天",checkout) and int(checkout[3:5])>=9:
                    mail_content.append(i.find_element_by_xpath(".//td[@class='gt tf']//u").text)
                    print i.find_element_by_xpath(".//td[@class='gt tf']//u").text
        self.driver.quit()
        return mail_content
 
def Analysis(mail_content,file_name):
    print u"现在开始分析有效邮件数据"
    try:
        book=load_workbook(file_name)
        book1=book.get_sheet_by_name("Sheet1")
    except Exception,e:
        print u"excel对象获取失败,错误是: %s" % e.message
    book1.cell(row=1,column=2,value=time.strftime("%Y%m%d",time.localtime(time.time()-24*60*60)))
    for j in range(2,book1.max_row+1):
        book1.cell(row=j,column=3,value=u"")
    for i in mail_content:
        for j in range(2,book1.max_row+1):
            if book1.cell(row=j,column=1).value in i:
                value=int(book1.cell(row=j,column=2).value)
                book1.cell(row=j,column=2,value=value+1)
                book1.cell(row=j,column=3,value=u"练习已提交")
    book.save(file_name)
 
def brower_object(browser): 
    if browser.lower()=="ie":
driver = webdriver.Ie()
        time.sleep(5)
        return driver
    elif browser.lower()=="firefox":
        driver=webdriver.Firefox()
        time.sleep(5)
        return driver
    else:
        print u"您输入的浏览器 %s 暂时还不支持驱动" % self.browser
 
if __name__=="__main__":
    from selenium import webdriver
    import time
    from openpyxl import load_workbook
    import re
    import sys
    reload(sys)
    sys.setdefaultencoding("utf-8")
    driver=brower_object("firefox")
    practice=Practice("http://www.mail.qq.com",driver)
    mail_content=practice.brower_content()
    name="1"
    file_name=u"c://统计//统计.xlsx"
    Analysis(mail_content,file_name)
原文地址:https://www.cnblogs.com/zhangtebie/p/11185933.html