上次使用selenium做了一个问卷星的自动答题程序:https://www.52pojie.cn/thread-1971878-1-1.html
大家给了很多建议,这次恰逢又要作新的问卷星,我试着用drissionpage写了自动答题脚本,和大家分享。
这次较为简单,题目都是固定的。
纯填空题:
[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
from DrissionPage import Chromium
import time
import os
import configparser as cp
def complete_exam(url, name, stnum):
# 创建浏览器对象
tab = Chromium().latest_tab
tab.get(url)
# 读取答案文件
with open('daan0.txt', 'r', encoding='utf-8') as file:
answers = file.readlines()
# 个人信息
name_ipt = tab.ele('@id=q1')
name_ipt.input(name)
stnum_ipt = tab.ele('@id=q2')
stnum_ipt.input(stnum)
fieldset = tab.eles('@class=field ui-field-contain')
qnum = fieldset.__len__()
# 遍历题目并填写答案
for i in range(1, qnum-1):
answer = answers[i - 1].strip()
print(answer)
qg = tab.ele(f'@id=div{i+2}')
ipt = qg.ele('@class=textCont')
ipt.input(answer)
# 点击提交按钮
submit_button = tab.ele('@id=ctlNext')
submit_button.click()
# 等待提交完成
time.sleep(5)
os.system('pause')
# 关闭浏览器
tab.close()
if __name__ == "__main__":
config_path = './config.ini'
inifile = cp.ConfigParser()
inifile.read(config_path, 'UTF-8')
url = inifile['db']['url']
name = inifile['db']['name']
stnum = inifile['db']['stnum'][1:]
complete_exam(url, name, stnum)
纯选择题:
[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
from DrissionPage import Chromium
import time
import os
import configparser as cp
if __name__ == "__main__":
config_path = './config.ini'
inifile = cp.ConfigParser()
inifile.read(config_path, 'UTF-8')
url = inifile['db']['url']
name = inifile['db']['name']
stnum = inifile['db']['stnum'][1:]
# 创建浏览器对象
tab = Chromium().latest_tab
tab.get(url)
# 读取答案文件
with open('daan1.txt', 'r', encoding='utf-8') as file:
answers = file.readlines()
# 个人信息
name_ipt = tab.ele('@id=q1')
name_ipt.input(name)
stnum_ipt = tab.ele('@id=q2')
stnum_ipt.input(stnum)
fieldset = tab.eles('@class=field ui-field-contain')
qnum = fieldset.__len__()
# 遍历题目并填写答案
for i in range(1, qnum-1):
answer = int(answers[i - 1].strip())
qo = tab.ele(f'@id=div{i+2}')
qo = qo.ele('@class=ui-controlgroup column1')
try:
options = qo.eles('@class=ui-radio')
options[answer-1].click()
except:
pass
# 点击提交按钮
submit_button = tab.ele('@id=ctlNext')
submit_button.click()
# 等待提交完成
time.sleep(5)
os.system('pause')
# 关闭浏览器
tab.close() |