Recently, there are a lot of outsourcing of automatic answering questions. Let me share with you how to use Python to realize automatic answering questions.
Well without further ado, let's get started.
First you need to prepare these
environment use
Python 3.8 解释器
Pycharm 编辑器
module use
import requests ---> 数据请求模块 pip install requests
import re
from selenium import webdriver ---> 自动测试模块 pip install selenium==3.141.0 <指定版本安装>
other tools
Google Chrome
Google Chrome Drive
Driver installation tutorial: Google Chrome
Automatic answering tool: selenium automated test module
selenium --> simulate human behavior to operate the browser
Normal answering process
- open browser
- visit website
- browse topics
- Choose what you think is the correct answer
for the program
- Browse the questions
<get the question answer question bank>
I. Get all the questions and answers
II. During the answering process, automatically get the answer content of this question
Each question has an answer analysis page:
https:/ .com/Post/9e209.htm
https:// .com/Post/d72d6.htm
https://***.com/Post/6f533.htm
is equivalent to the page ID --> Get the ID of 50 questions and build the answer page url for 50 questions address - Choose the answer you think is correct
Compare the answer with the options and choose the same
Keywords: tiba.jsyks
Code combat
module
# selenium 需要安装 pip install selenium==3.141.0
from selenium import webdriver
# 导入数据请求模块
import requests
# 导入正则表达式
import re
open browser
# <selenium.webdriver.chrome.webdriver.WebDriver (session="c55234aeab1503a9ab7ba8a4dd7bf457")>
driver = webdriver.Chrome()
# 访问网址
driver.get('https://www.***.com/kms-mnks')
# 最大化浏览器
driver.maximize_window()
selenium get data
# 通过 CSS 选择器查找元素
lis = driver.find_elements_by_css_selector('div.Exam ul li')
# for循环遍历
for li in lis:
# get_attribute 获取标签属性
answer_id = li.get_attribute('c')
send request
# 请求链接
link = f'https://**.***.com/Post/{
answer_id}.htm'
# 模拟 伪装浏览器 <请求头>
headers = {
# User-Agent 用户代理 表示浏览器基本身份信息
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}
# 发送请求, 获取响应文本数据
html_data = requests.get(link, headers=headers).text
Analytical data
answer = re.findall('br/>答案:<u>(.*?)</u>', html_data)[0]
Get options tab
bs = li.find_elements_by_css_selector('b')
for loop traversal
for b in bs:
# 获取选项内容
choose = b.text
# 对于选项进行处理 判断选择是否正确
if choose == '正确':
# 重新赋值为对
choose = '对'
elif choose == '错误':
choose = '错'
# len内置函数, 统计元素个数
elif len(choose) > 2: # 判断当选项内容元素大于2的时候
# [0] 根据索引位置取值, 提取第一个元素
choose = choose[0]
for a in answer:
# 当选项和答案相等时候, 进行点击操作
if choose == a:
b.click()
submit test paper
driver.find_element_by_class_name('btn_JJ').click()
At this point, we can happily answer questions automatically, which is quite simple. If you don’t know how to operate, I have a detailed operation video, which is packaged together with the source code. You can get a business card at the end of the article.
Well, today's sharing is over here, see you next time!