Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3261277
  • 博文数量: 287
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2110
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(287)

文章存档

2022年(60)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2021-03-12 17:28:33

import time

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys

import smtplib

from email.mime.text import MIMEText

from email.header import Header

# 邮箱设置

class Mail:

    def __init__(self, receivers):

        # 第三方 SMTP 服务

        self.mail_host = "smtp.qq.com"  # 设置服务器:这个是qq邮箱服务器

        self.mail_pass = "***************"  # 授权码  需到服务提供商处获取

        self.sender = '*************'  # 你的邮箱地址

        self.receivers = receivers  # 收件人的邮箱地址,可设置为你的QQ邮箱或者其他邮箱,可多个

    def send(self, subject, content, sender_name, receivers_name):

        message = MIMEText(content, 'plain', 'utf-8')

        message['From'] = Header(sender_name, 'utf-8')

        message['To'] = Header(receivers_name[0], 'utf-8')  # 收件人名字,但只能传字符串,因此取列表第一个

        message['Subject'] = Header(subject, 'utf-8')

        try:

            smtpObj = smtplib.SMTP_SSL(self.mail_host, 465)

            smtpObj.login(self.sender, self.mail_pass)

            smtpObj.sendmail(self.sender, self.receivers, message.as_string())

            smtpObj.quit()

            print('Email sent successfully')

        except smtplib.SMTPException as e:

            print('Failed to send Email')

# 这里对浏览器进行设置,防止在后面find函数报错

WIDTH = 600  # 宽度

HEIGHT = 1000  # 高度

PIXEL_RATIO = 3.0  # 分辨率

mobileEmulation ={"deviceMetrics": {"width": WIDTH, "height": HEIGHT, "pixelRatio": PIXEL_RATIO}}

options = webdriver.ChromeOptions()

options.add_experimental_option('mobileEmulation', mobileEmulation)

url = '**************' # 登录网址

def user(name, uid, password, email):

    try:

        browser = webdriver.Chrome(chrome_options=options)

        time.sleep(1)

        # 填写索要签到的网站地址

        browser.get(url)

        # 检查网页是否加载成功

        browser.find_element_by_xpath('//*[@placeholder="用户名"]').send_keys(uid)

        try:

            time.sleep(1)

            # 填写登录的用户名

            browser.find_element_by_xpath('//*[@placeholder="密码"]').send_keys(password)

            # 用户登录密码

            browser.find_element_by_xpath('//*[@placeholder="密码"]').send_keys(Keys.ENTER)

            time.sleep(15)

            # 便捷服务

            browser.find_element_by_xpath(

'//*[@id="wrapper"]/div[1]/div[1]/div/div[2]/div[2]/div[1]/div[5]/div/div/div[1]/div/div[1]/div/div/div[1]/div/div[4]').click()

            time.sleep(1)

            # 健康申报

            browser.find_element_by_xpath(

'//*[@id="wrapper"]/div[1]/div[1]/div/div[2]/div[2]/div[1]/div[5]/div/div/div[1]/div/div[1]/div/div/div[1]/div[2]/div/div[1]/div[3]').click()

            time.sleep(15)

            browser.switch_to.frame(1)

            # 37度以下

            browser.find_element(By.CSS_SELECTOR, "#D17-0-0 .fr-group-span:nth-child(1) .x-text").click()

            time.sleep(0.5)

            # 37度以下

            browser.find_element(By.CSS_SELECTOR, "#D18-0-0 .fr-group-span:nth-child(1) .x-text").click()

            time.sleep(0.5)

            # 绿色

            browser.find_element(By.CSS_SELECTOR, "#F20-0-0 .fr-group-span:nth-child(1) .x-text").click()

            time.sleep(0.5)

            #

            browser.find_element(By.CSS_SELECTOR, "#F21-0-0 .fr-group-span:nth-child(2) .x-text").click()

            time.sleep(0.5)

            #

            browser.find_element(By.CSS_SELECTOR, "#F22-0-0 .fr-group-span:nth-child(2) .x-text").click()

            time.sleep(0.5)

            #

            browser.find_element(By.CSS_SELECTOR, "#F23-0-0 .fr-group-span:nth-child(2) .x-text").click()

            time.sleep(0.5)

            #

            browser.find_element(By.CSS_SELECTOR, "#F25-0-0 .fr-group-span:nth-child(2) .x-text").click()

            time.sleep(0.5)

            #

            browser.find_element(By.CSS_SELECTOR, "#F27-0-0 .fr-group-span:nth-child(1) .x-text").click()

            time.sleep(0.5)

            #

            browser.find_element(By.CSS_SELECTOR, "#F29-0-0 .fr-group-span:nth-child(2) .x-text").click()

            time.sleep(0.5)

            #

            browser.find_element(By.CSS_SELECTOR, "#F32-0-0 .fr-group-span:nth-child(2) .x-text").click()

            time.sleep(1)

            # 提交

            browser.find_element(By.ID, "fr-btn-").click()

            time.sleep(2)

            # 检验是否提交成功

            final_text = browser.find_element_by_xpath('//*[@class="fh tac bw f16-0 pl2 b0"]').text

            print(final_text)

            browser.quit()

            if final_text == '填报成功':

                receivers = [email]  # 收件人列表

                subject = final_text  # 发送的主题

                content = '填报成功'  # 发送的内容

                sender_name = name  # 发件人姓名

                receivers_name = [name]  # 收件人姓名

                mail = Mail(receivers)

                mail.send(subject, content, sender_name, receivers_name)

        except:

            print('填报失败')

            final_text = '填报失败'

            receivers = [email]  # 收件人列表

            subject = final_text  # 发送的主题

            content = final_text  # 发送的内容

            sender_name = name  # 发件人姓名

            receivers_name = name  # 收件人姓名

 

            mail = Mail(receivers)

            mail.send(subject, content, sender_name, receivers_name)

    except:

        final_text = '网络连接超时'

        print(final_text)

        receivers = [email]  # 收件人列表

        subject = final_text  # 发送的主题

        content = final_text  # 发送的内容

        sender_name = name  # 发件人姓名

        receivers_name = name  # 收件人姓名

        mail = Mail(receivers)

        mail.send(subject, content, sender_name, receivers_name)

def main():

    name = '***'  # 名字

    uid = '**********'  # 学号

    password = '*****'  # 身份证后六位

    email = '*********'  # 接收的邮箱

    user(name, uid, password, email)

if __name__ == '__main__':

    main()

阅读(739) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~