python+selenium+至善网

python+selenium+至善网

使用python的第三方库selenium操作Chrome浏览器自动化完成至善网的学习

本文源码:https://gitee.com/ioucon/python-selenium

一、准备工作

  1. 安装 python 3.7

  2. 安装 selenium

  3. 下载 Chromedriver

  4. 安装 requests

二、主要代码

1. 登录

1.1 需要手动输入验证码
  • 版本①

    driver.get("http://www.attop.com/index.htm")
    sleep(1)
    def login(username, password):
        driver.find_element_by_xpath("/html/body/div[2]/div/div/ul/li[4]/a[1]").click()
        sleep(1)
        driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="pageiframe"]'))
        driver.find_element_by_name("username").send_keys(username)
        driver.find_element_by_name("password").send_keys(password)
        rand = input("输入验证码:")
        driver.find_element_by_name("rand").send_keys(rand)
        driver.find_element_by_class_name("btn_logon").click()
        sleep(1)
    
  • 版本②

    driver.get("http://www.attop.com/login_pop.htm")
    sleep(1)
    def login(username, password):
        driver.find_element_by_name("username").send_keys(username)
        driver.find_element_by_name("password").send_keys(password)
        rand = input("输入验证码:")
        driver.find_element_by_name("rand").send_keys(rand)
        driver.find_element_by_class_name("btn_logon").click()
        sleep(1)
    

    ②相比于①,直接通过登录入口,减少了多余的步骤。

1.2 接入打码平台

2. 习题

def doXt(data):
    xt_li_list = driver.find_elements_by_xpath('//*[@id="showajaxinfo"]/div[2]/dl/dd[1]/ul/li')
    sleep(1)
    for xt_li in xt_li_list:
        xtName = xt_li.find_element_by_xpath('.//p').text
        ans = data.get(xtName)
        if ans == None:
            continue
        op_li_list = xt_li.find_elements_by_xpath('.//ul/li')
        try:
            statu = driver.find_element_by_xpath('//*[@id="xt_18001"]/h5/span/strong').text 
            if statu == '答题正确':
                continue
            elif statu == '待回答':
                for op_li in op_li_list:
                    if op_li.text in ans:
                        if op_li.find_element_by_xpath('./input').get_attribute('checked') != "true":
                            op_li.find_element_by_xpath('./input').click()
            else:
                continue
        except:
            for op_li in op_li_list:
                if op_li.text in ans:
                    if op_li.find_element_by_xpath('./input').get_attribute('checked') != "true":
                        op_li.find_element_by_xpath('./input').click()
    try:    
        driver.find_element_by_xpath('//dd[@class="alignCenter"]/a[@class="btn100_org"]').click()
        sleep(1)
    	driver.find_element_by_xpath('//div/button[1]').click()
    except:
        pass

3. 媒材评价

def doMshow():
    p_list = driver.find_elements_by_xpath('//*[@id="showajaxinfo"]/div[1]/dl/dd/p')
    sleep(1)
    for p in p_list:
        try:
            pj = p.find_element_by_class_name('BT_ping').text
            if  pj == "马上评价":
                p.find_element_by_xpath('./a').click()
                driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="pageiframe"]'))
                sleep(1)
                driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/p/input[3]').click()
                driver.find_element_by_xpath('/html/body/div[1]/div//tr[2]/td[2]/div//tr[3]/td/div/button[1]').click()
                sleep(1)
                # 此处注意:由于上面切换到frame,这里需要切回原页面
                driver.switch_to_default_content()
                driver.find_element_by_xpath("//div/a[@class='aui_close']").send_keys(Keys.ENTER)
                # 递归的方法速度太慢,还需要进一步完善
                doMshow()
        except:
            continue

4. 方糖推送

前提:登录方糖推送,获取SCKEY,并绑定自己的微信
方糖:http://sc.ftqq.com/3.version
把自己SCKEY填入下方key中

def sendMessage(username,password,coursename,score):
    key = ''

    url = 'https://sc.ftqq.com/{}.send'.format(key)

    data = {
        'text':'至善网:'+username+'\n当前分数:'+score,
        'desp':'''
# 至善网:
#### 账号:{}
#### 密码:{}
#### 科目:{}
#### 分数:{}
'''.format(username,password,coursename,score)
    }
    requests.post(url,data)

加入方糖后,可以通过微信接收到完成提醒,不需要一直盯着电脑。

——未完,有空再更新

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BIN丶虫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值