网页自动填写

网页自动填写

  • 前言
  • 效果
  • 使用教程
  • 文件下载
  • 源码

前言

最近看学弟在写视听说的网课,想起来我在当时懒得一个一个填,就写了一个自动填写的程序。学校这个网站的题目应该不会更新,所以应该能一届一届用下去吧2333

效果

使用教程

本程序借助的是python的selenium库调用chrome进行操作,所以需要一个谷歌官方提供的api接口,就是这个chromedriver,每个版本的chrome需要使用对应的chromedriver,将chromedriver这个文件夹里面的文件替换就好了。

网页自动填写_第1张图片

这里可以查看chrome的版本
网页自动填写_第2张图片

运行里面这个视听说.exe输入账号密码单元就能开始自动填写了。
网页自动填写_第3张图片

文件下载

链接:https://pan.baidu.com/s/1poQXcnFmDPOjufrpdVwLMg
提取码:x274
这里面除了视听说,还有大二下学期的工程英语视听说,操作方法是一样的。

源码

网页填写代码

from selenium import webdriver
from extract_answer import *
from retrying import retry
import time
import re


__author__ = 'ghost222'


class shitingshuo():
    def __init__(self, account, password):
        # self.answer=extract_answer(unit)
        self.driver = webdriver.Chrome('./chromedriver/chromedriver.exe')
        self.account = account
        self.password = password
        # self.unit=unit

    def preparation(self):
        self.driver.get('http://202.119.161.130/common/index.aspx')
        try:
            self.driver.find_element_by_id('ctl00_ContentPlaceHolder1_UcLogin1_txt_UserName')\
                .send_keys(self.account)
            self.driver.find_element_by_id('ctl00_ContentPlaceHolder1_UcLogin1_txt_PassWord')\
                .send_keys(self.password)
            self.driver.find_element_by_id('ctl00_ContentPlaceHolder1_UcLogin1_ibtn_ok')\
                .click()
        except:
            self.driver.get(
                'http://202.119.161.130/CourseLearning/ClassroomOnline.aspx')
        self.driver.find_element_by_xpath('//*[@id="ctl00_ContentPlaceHolder1_div_ok"]/table/tbody/tr[5]/td/table/tbody/tr[2]/td[3]/a')\
            .click()
        self.select_unit()
        self.driver.find_element_by_link_text('Task 1').click()

    def select_unit(self):
        temp_unit_xpath = '//*[@id="aspnetForm"]/table/tbody/tr/td[2]/table[1]/tbody/tr[3]/td/div/span/table/tbody/tr[{0}]/td[{1}]/a'
        unit_xpath = temp_unit_xpath.format(
            (self.unit+1)//2, ((self.unit+1) % 2+1)*2)
        self.driver.find_element_by_xpath(unit_xpath).click()

    @retry(stop_max_attempt_number=4)# 重复尝试
    def do_task1(self):
        ans = self.answer.task1()
        #try:
        for a in ans:
            self.driver.find_element_by_name(ans.index(a)).send_keys(a)
        time.sleep(5)   # 赞停5秒,防止服务器检测
        try:
            self.driver.find_element_by_name(
                'ctl00$ContentPlaceHolder1$submit').click()
            a1 = self.driver.switch_to.alert
            a1.accept()
        except:# 跳过已做的Task
                pass
        finally:
            self.driver.find_element_by_link_text('Next').click()
        # except:# 跳过非做题页
        #     self.driver.find_element_by_link_text('Next').click()
        #     raise Exception()

    @retry(stop_max_attempt_number=3)# 重复尝试
    def do_task2(self):
        ans = self.answer.task2()
        try:
            for a in ans:
                self.driver.find_element_by_name(ans.index(a)).send_keys(a)
            time.sleep(5)   # 赞停5秒,防止服务器检测
            try:
                self.driver.find_element_by_name(
                    'ctl00$ContentPlaceHolder1$submit').click()
                a1 = self.driver.switch_to.alert
                a1.accept()
            except:# 跳过已做的Task
                    pass
            finally:
                self.driver.find_element_by_link_text('Next').click()
        except:# 跳过非做题页
            self.driver.find_element_by_link_text('Next').click()
            raise Exception()

    @retry(stop_max_attempt_number=3)# 重复尝试
    def do_task3(self):
        ans = self.answer.task3()
        try:
            for a in ans:
                self.driver.find_element_by_name(ans.index(a)).send_keys(a)
            time.sleep(5)   # 赞停5秒,防止服务器检测
            try:
                self.driver.find_element_by_name(
                    'ctl00$ContentPlaceHolder1$submit').click()
                a1 = self.driver.switch_to.alert
                a1.accept()
            except:# 跳过已做的Task
                    pass
            finally:
                self.driver.find_element_by_link_text('Next').click()
        except:# 跳过非做题页
            self.driver.find_element_by_link_text('Next').click()
            raise Exception()

    @retry(stop_max_attempt_number=3)# 重复尝试
    def do_task4(self):
        ans = self.answer.task4()
        try:
            for a in ans:
                self.driver.find_element_by_name(ans.index(a)).send_keys(a)
            time.sleep(5)   # 赞停5秒,防止服务器检测
            try:
                self.driver.find_element_by_name(
                    'ctl00$ContentPlaceHolder1$submit').click()
                a1 = self.driver.switch_to.alert
                a1.accept()
            except:# 跳过已做的Task
                    pass
            finally:
                self.driver.find_element_by_link_text('Next').click()
        except:# 跳过非做题页
            self.driver.find_element_by_link_text('Next').click()
            raise Exception()

    @retry(stop_max_attempt_number=3)# 重复尝试
    def do_task5(self):
        ans = self.answer.task5()
        try:
            self.driver.find_element_by_xpath(
                '//*[@id="div1"]/div/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td/textarea').send_keys(ans[0])
            self.driver.find_element_by_xpath(
                '//*[@id="div1"]/div/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[4]/td/textarea').send_keys(ans[0])
            time.sleep(5)   # 赞停5秒,防止服务器检测
            try:
                self.driver.find_element_by_name(
                    'ctl00$ContentPlaceHolder1$submit').click()
                a1 = self.driver.switch_to.alert
                a1.accept()
            except:# 跳过已做的Task
                    pass
            finally:
                self.driver.find_element_by_link_text('Next').click()
        except:# 跳过非做题页
            self.driver.find_element_by_link_text('Next').click()
            raise Exception()

    @retry(stop_max_attempt_number=3)# 重复尝试
    def do_task6(self):
        try:
            ans = self.answer.task6()
            for a in ans:
                temp_xpath = '//*[@id="div1"]/div/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[{}]/td/textarea'.format(
                    (ans.index(a)+1)*2)
                self.driver.find_element_by_xpath(temp_xpath).send_keys(a)
            time.sleep(5)   # 赞停5秒,防止服务器检测
            self.driver.find_element_by_name(
                'ctl00$ContentPlaceHolder1$submit').click()
            try:
                a1 = self.driver.switch_to.alert
                a1.accept()
            finally:
                pass
            self.driver.find_element_by_link_text('Next').click()
        except Exception:
            self.driver.find_element_by_link_text('Next').click()

    @retry(stop_max_attempt_number=1)# 重复尝试
    def do_task7(self):
        try:
            ans = self.answer.task7()
            for a in ans:
                self.driver.find_element_by_name(ans.index(a)).send_keys(a)
            self.driver.find_element_by_name(
                'ctl00$ContentPlaceHolder1$submit').click()
            time.sleep(5)   # 赞停5秒,防止服务器检测
            try:
                a1 = self.driver.switch_to.alert
                a1.accept()
            except:# 跳过已做的Task
                    pass
            finally:
                self.driver.find_element_by_link_text('Next').click()
        except:# 跳过非做题页
            self.driver.find_element_by_link_text('Next').click()

    @retry(stop_max_attempt_number=3)# 重复尝试
    def do_task8(self):
        ans = self.answer.task8()
        try:
            for a in ans:
                self.driver.find_element_by_name(ans.index(a)).send_keys(a)
            time.sleep(5)   # 赞停5秒,防止服务器检测
            try:
                self.driver.find_element_by_name(
                    'ctl00$ContentPlaceHolder1$submit').click()
                a1 = self.driver.switch_to.alert
                a1.accept()
            except:# 跳过已做的Task
                    pass
            finally:
                self.driver.find_element_by_link_text('Next').click()
        except:# 跳过非做题页
            self.driver.find_element_by_link_text('Next').click()
            raise Exception()

    @retry(stop_max_attempt_number=3)# 重复尝试
    def do_task9(self):
        ans = self.answer.task9()
        try:
            for a in ans:
                temp_xpath = '//*[@id="div1"]/div/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr//*[@value=\"{}\"]'.format(
                    a)
                self.driver.find_element_by_xpath(temp_xpath).click()
            time.sleep(5)   # 赞停5秒,防止服务器检测
            try:
                self.driver.find_element_by_name(
                    'ctl00$ContentPlaceHolder1$submit').click()
                a1 = self.driver.switch_to.alert
                a1.accept()
            except:# 跳过已做的Task
                pass
            finally:
                self.driver.find_element_by_link_text('Next').click()
        except:# 跳过非做题页
            self.driver.find_element_by_link_text('Next').click()
            raise Exception()

    @retry(stop_max_attempt_number=3)# 重复尝试
    def do_task10(self):
        ans = self.answer.task10()
        count = 0
        temp_xpath = '//*[@id="div1"]/div/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[{}]/td/input'
        index = {'A': 2, 'B': 3, 'C': 4, 'D': 5}
        try:
            for a in ans:
                xpath = temp_xpath.format(index[a]+5*count)
                self.driver.find_element_by_xpath(xpath).click()
                count += 1
            time.sleep(5)   # 赞停5秒,防止服务器检测
            try:
                self.driver.find_element_by_name(
                    'ctl00$ContentPlaceHolder1$submit').click()
                a1 = self.driver.switch_to.alert
                a1.accept()
            except:# 跳过已做的Task
                    pass
            finally:
                self.driver.find_element_by_link_text('Next').click()
        except:# 跳过非做题页
            self.driver.find_element_by_link_text('Next').click()
            raise Exception()

    @retry(stop_max_attempt_number=3)# 重复尝试
    def do_task11(self):
        ans = self.answer.task11()
        try:
            for a in ans:
                temp_xpath = '//*[@id="div1"]/div/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[{}]/td/textarea'.format(
                    (ans.index(a)+1)*2)
                self.driver.find_element_by_xpath(temp_xpath).send_keys(a)
            time.sleep(5)   # 赞停5秒,防止服务器检测
            try:
                self.driver.find_element_by_name(
                    'ctl00$ContentPlaceHolder1$submit').click()
                a1 = self.driver.switch_to.alert
                a1.accept()
            except:# 跳过已做的Task
                    pass
            finally:
                self.driver.find_element_by_link_text('Next').click()
        except:# 跳过非做题页
            self.driver.find_element_by_link_text('Next').click()
            raise Exception()

    @retry(stop_max_attempt_number=3)# 重复尝试
    def do_task14(self):
        ans = self.answer.task14()
        try:
            for a in ans:
                self.driver.find_element_by_name(ans.index(a)).send_keys(a)
            time.sleep(5)   # 赞停5秒,防止服务器检测
            try:
                self.driver.find_element_by_name(
                    'ctl00$ContentPlaceHolder1$submit').click()
                a1 = self.driver.switch_to.alert
                a1.accept()
            except:# 跳过已做的Task
                    pass
            finally:
                self.driver.find_element_by_link_text('Next').click()
        except:# 跳过非做题页
            self.driver.find_element_by_link_text('Next').click()
            raise Exception()

    @retry(stop_max_attempt_number=3)# 重复尝试
    def do_task15(self):
        ans = self.answer.task15()
        try:
            for a in ans:
                self.driver.find_element_by_name(ans.index(a)).send_keys(a)
            time.sleep(5)   # 赞停5秒,防止服务器检测
            try:
                self.driver.find_element_by_name(
                    'ctl00$ContentPlaceHolder1$submit').click()
                a1 = self.driver.switch_to.alert
                a1.accept()
            except:# 跳过已做的Task
                    pass
        except:# 跳过非做题页
            self.driver.find_element_by_link_text('Next').click()
            raise Exception()

    def run(self, unit):
        self.answer = extract_answer(unit)
        self.unit = unit
        self.preparation()
        # for num in range(3):
        #     self.driver.find_element_by_link_text('Next').click()
        self.do_task1()
        self.do_task2()
        self.do_task3()
        self.do_task4()
        self.do_task5()
        self.do_task6()
        self.do_task7()
        self.do_task8()
        self.do_task9()
        self.do_task10()
        self.do_task11()
        self.do_task14()
        self.do_task15()


if __name__ == '__main__':
    account = input('输入账号:')
    password = input('输入密码:')
    units = input('输入单元(用逗号隔开,如:1,2,3):')
    units = [int(i) for i in units.split(',')]
    sts = shitingshuo(account, password)
    for i in units:
        sts.run(i)

从txt中提取答案的代码,利用正则表达式提取的。

import re
import sys
import os
# os.chdir(sys.path[0])


class extract_answer():
    def __init__(self, unit):
        self.unit = unit
        self.file = open('.\视听说答案.txt', 'r', encoding='utf-8')
        self.content = self.search_unit(unit)

    def search_unit(self, unit):
        if unit > 10 or unit < 1:
            raise Exception('无效unit')
        while True:
            line = self.file.readline()
            if 'U'+str(unit) in line:
                break
        content = self.file.read()
        if unit == 10:
            total_content = re.search('Task 1(.+)', content, re.S).group(0)
        elif unit > 0 and unit < 10:
            total_content = re.search(
                'Task 1(.+?)'+'U'+str(unit+1), content, re.S).group(0)
        return total_content

    def task1(self):
        content = re.search('Task 1(.+?)Task 2', self.content, re.S).group()
        answer = re.findall('\(\s*(\w+?)\s*\) \[', content, re.S)
        if not answer:
            raise Exception('task1未找到答案')
        return answer

    def task2(self):
        content = re.search('Task 2(.+?)Task 3', self.content, re.S).group()
        answer = re.findall('\((.+?)\)', content, re.S)
        if not answer:
            raise Exception('task2未找到答案')
        return answer

    def task3(self):
        content = re.search('Task 3(.+?)Task 4', self.content, re.S).group()
        answer = re.findall('\((.+?)\)', content, re.S)
        if not answer:
            raise Exception('task3未找到答案')
        return answer

    def task4(self):
        content = re.search('Task 4(.+?)Task 5', self.content, re.S).group()
        answer = re.findall('\)\s*\((.+?)\)', content, re.S)
        if not answer:
            raise Exception('task4未找到答案')
        return answer

    def task5(self):
        content = re.search('Task 5(.+?)Task 6', self.content, re.S).group()
        answer = re.findall('Reference answer: (.+?)\.', content, re.S)
        if not answer:
            raise Exception('task5未找到答案')
        return answer

    def task6(self):
        content = re.search('Task 6(.+?)Task 7', self.content, re.S).group()
        answer = re.findall('Reference answer:[\s]*(.+?)\.', content, re.S)
        if not answer:
            raise Exception('task6未找到答案')
        return answer

    def task7(self):
        content = re.search('Task 7(.+?)Task 8', self.content, re.S).group()
        answer = re.findall('\)  \((.+?)\)', content, re.S)
        if not answer:
            raise Exception('task7未找到答案')
        return answer

    def task8(self):
        content = re.search('Task 9(.+?)Task 9', self.content, re.S).group()
        answer = re.findall('\((.+?)\)', content, re.S)
        if not answer:
            raise Exception('task8未找到答案')
        return answer

    def task9(self):
        content = re.search('Task 9(.+?)Task 9(.+?)Task 10',
                            self.content, re.S).group(2)
        answer = re.findall('\((.+?)\)', content, re.S)
        if not answer:
            raise Exception('task9未找到答案')
        return answer

    def task10(self):
        content = re.search('Task 10(.+?)Task 11', self.content, re.S).group()
        answer = re.findall('[\.|\?][\s]*\((.+?)\)', content, re.S)
        if not answer:
            raise Exception('task10未找到答案')
        return answer

    def task11(self):
        content = re.search('Task 11(.+?)Task (12|14)',
                            self.content, re.S).group()
        answer = re.findall('Reference answer: (.+?)\.', content, re.S)
        if not answer:
            raise Exception('task11未找到答案')
        return answer

    def task14(self):
        content = re.search('Task 14(.+?)Task 15', self.content, re.S).group()
        answer = re.findall('\((.+?)\)', content, re.S)
        if not answer:
            raise Exception('task14未找到答案')
        return answer

    def task15(self):
        content = re.search('Task 15(.+)', self.content, re.S).group()
        answer = re.findall('\)  \((.+?)\)', content, re.S)
        if not answer:
            raise Exception('task15未找到答案')
        return answer


if __name__ == '__main__':
    a = extract_answer(9)
    b = a.task8()
    print(b, len(b))

你可能感兴趣的:(python,爬虫,selenium)