[There are endless homework exams at the beginning of the semester] Python automatic answer script, answer questions online, you can learn it with zero foundation! ! (Source code attached)

Preface

Hello everyone, I am Xiaoman~
I think most students have already started school in September. When school starts, teachers will assign some homework on software. Today I will teach you how to use Python to create an automatic answer script, with 100% accuracy. If you like it, please remember to follow and collect it
. Oh~
Insert image description here

Environmental use

  • Python 3.8
  • Pycharm

Module usage

  • import requests —> data request module pip install requests
  • import parsel —> data parsing module pip install parsel
  • from selenium import webdriver —> Automatic test module pip install selenium==3.141.0

The code implementation ideas for this case:

1. Open the exam website
selenium --> browser driver --> operate the browser <simulate human behavior and operate the browser>
2. Get the answer.
Get the answer website link to get the question and answer content.
3. Compare the questions and answers and choose the correct one. Answer
Get the answer options of the question
and compare it with the correct answer.
If the correct answer is consistent with the selected answer, click
4. Click to answer the question

final effect
Insert image description here

Code

import module

源码获取可以加一下wx:‍python10010 发送验证时记得备注 “M”噢 
from selenium import webdriver
# 导入数据请求模块
import requests
# 导入数据解析模块
import parsel

Open the browser webdriver.Chrome('driver path')

1. Put the driver and the code together
2. Put the driver file and the python installation directory together

driver = webdriver.Chrome(r'D:\download\anaconda\chromedriver.exe')
# 设置全屏
driver.maximize_window()

Enter URL

driver.get('https://www.jsyks.com(mm)/kmy-mnks')

Get questions and answers

lis = driver.find_elements_by_css_selector('div.Exam ul li')
page = 1
# for循环遍历, 提取列表里面的元素
for li in lis:
    # 获取属性
    answer_id = li.get_attribute('c')  #  答案链接的ID
    # https://tiba.jsyks.com/Post/8f0e0.htm
    answer_url = f'https://tiba.jsyks.com/Post/{answer_id}.htm'
    # 获取答案以及问题  --> <Response [200]> 表示请求成功
    response = requests.get(url=answer_url)
    # 获取网页内容 --> 提取答案以及问题内容  转换数据类型
    selector = parsel.Selector(response.text)
    # 获取问题 --> 把问题和答案保存数据库保存 直接比较答案, 自己建立问题库, 从问题找答案
    question = selector.css('#question h1 strong a::text').get()
    # 获取答案
    answer = selector.css('#question h1 u::text').get()

Click to judge

if answer == '错':
    # 把答案内容改成 错误  重新赋值一下
    answer = '错误'
#  # 如果答案内容是对的话
elif answer == '对':
    # 把答案内容改成正确重新赋值一下
    answer = '正确'
# 获取问题选项内容 --> b标签 全部获取下来  b标签是在li标签里面  driver 表示整个网页
bs = li.find_elements_by_css_selector('b')
num = 1
for b in bs:
    # 获取b标签文本
    choose = b.text
    # 判断如何答案比两个元素多的, 就取一个元素
    if len(choose) > 2:
        # [0]提取 字符串里面第一个元素
        choose = choose[0]
    # 进行对比 选项和答案一致 是否一模一样
    if choose == answer:
        # 点击选项 --> 定位到点击那个元素 LI1 --> 1 表示第一题  b:nth-child(3) 表示第一个答案
        driver.find_element_by_css_selector(f'#LI{page} b:nth-child({2+num})').click()
        print('点击了')
    else:
        print('选项是', choose, '答案是', answer)
    print(choose)
    num +=1
page += 1

Click to submit exam paper

If it is too fast, the program may not have found the element yet. <The web page has not loaded the element yet, so you click directly and an error may be reported>

driver.find_element_by_css_selector('div.ExamBtn u.btnJJ').click()

Guess you like

Origin blog.csdn.net/XM67_/article/details/132641455