[Source code can be shared] Teach you to use Python to make an automatic answering script to achieve automatic answering, 100% correct rate!


foreword

In today's society, people's lives are increasingly dependent on computer technology, and Python, as a high-level programming language, has become the language of choice for many programmers. Python has the characteristics of easy learning, concise code, and strong readability, so it is widely used in various fields. Among them, automated scripting is one of the important application areas of Python. This article will introduce how to use Python to write an automatic answering script.


Click here to jump to receive

Relevant information files and code files can be found directly at the end of the article~ Remember to like and support it~

insert image description here

1. The principle of automatic answering questions

The principle of automatic question answering is to automatically complete the answering process by simulating human operation through the program. Specifically, the question and options are obtained through the program, and then the correct answer is calculated according to a certain algorithm, and the answer is automatically filled in the answer interface.
insert image description here

2. Steps of automatic answering questions

The steps of automatic answering can be divided into the following parts:

  1. Get questions and options

Obtaining questions and options is the first step in automatic answering. Generally speaking, there are two ways to obtain questions and options: one is to recognize the text on the screen through OCR technology, and the other is to obtain questions and options through a web crawler.

  1. Calculate the correct answer

After obtaining the questions and options, it is necessary to calculate the correct answer. There are many ways to calculate the correct answer, such as querying the answer through a search engine, classifying through a machine learning algorithm, and so on.

  1. autocomplete answer

After calculating the correct answer, the answer needs to be automatically filled in the answer interface. This step can be realized by simulating keyboard input.

Three, Python to achieve automatic answering method

There are many ways to automatically answer questions in Python. Here are two commonly used methods.

  1. Use PyAutoGUI library to realize automatic answering

PyAutoGUI is a Python library that can simulate mouse and keyboard operations and can be used to implement automated scripts. The steps to use PyAutoGUI to realize automatic answering are as follows:

(1) Install the PyAutoGUI library

Enter the following command at the command line to install the PyAutoGUI library:

pip install pyautogui

(2) Get the title and options

The method of using the PyAutoGUI library to obtain the title and options is as follows:

import pyautogui

# 获取题目和选项的位置
question_pos = (100, 100)
option1_pos = (100, 200)
option2_pos = (100, 300)
option3_pos = (100, 400)

# 获取题目和选项的文字
question_text = pyautogui.screenshot(region=(question_pos[0], question_pos[1], 800, 100))
option1_text = pyautogui.screenshot(region=(option1_pos[0], option1_pos[1], 800, 100))
option2_text = pyautogui.screenshot(region=(option2_pos[0], option2_pos[1], 800, 100))
option3_text = pyautogui.screenshot(region=(option3_pos[0], option3_pos[1], 800, 100))

(3) Calculate the correct answer

The method of calculating the correct answer can be selected according to the specific situation. For example, you can query the answer through a search engine, the code is as follows:

import requests
from bs4 import BeautifulSoup

# 搜索答案
def search_answer(question):
    url = 'https://www.baidu.com/s'
    params = {
    
    'wd': question}
    headers = {
    
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
    response = requests.get(url, params=params, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    answer = soup.find('div', {
    
    'class': 'c-abstract'}).text
    return answer

(4) Automatically fill in answers

Here's how to automatically fill in answers using the PyAutoGUI library:

# 自动填写答案
def fill_answer(answer):
    answer_pos = (100, 500)
    pyautogui.click(answer_pos)
    pyautogui.typewrite(answer)
    pyautogui.press('enter')
  1. Using the Selenium library to realize automatic answering of questions
    Selenium is an automated testing tool that can simulate the operation of a browser and can be used to implement automated scripts. The steps to implement automatic answering using the Selenium library are as follows:

(1) Install the Selenium library

Enter the following command at the command line to install the Selenium library:

pip install selenium

(2) Start the browser

Here's how to start a browser using the Selenium library:

from selenium import webdriver

# 启动浏览器
driver = webdriver.Chrome()

(3) Open the answering website

The method of using the Selenium library to open the answering website is as follows:

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

(4) Get the title and options

The way to get questions and options using the Selenium library is as follows:

# 获取题目和选项的文字
question_text = driver.find_element_by_xpath('//div[@class="question"]/p').text
option1_text = driver.find_element_by_xpath('//div[@class="options"]/ul/li[1]/label').text
option2_text = driver.find_element_by_xpath('//div[@class="options"]/ul/li[2]/label').text
option3_text = driver.find_element_by_xpath('//div[@class="options"]/ul/li[3]/label').text

(5) Calculate the correct answer

The method of calculating the correct answer can be selected according to the specific situation. For example, you can query the answer through a search engine, the code is as follows:

import requests
from bs4 import BeautifulSoup

# 搜索答案
def search_answer(question):
    url = 'https://www.baidu.com/s'
    params = {
    
    'wd': question}
    headers = {
    
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
    response = requests.get(url, params=params, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    answer = soup.find('div', {
    
    'class': 'c-abstract'}).text
    return answer

(6) Automatically fill in answers

Here's how to autofill answers using the Selenium library:

# 自动填写答案
def fill_answer(answer):
    answer_input = driver.find_element_by_xpath('//input[@type="text"]')
    answer_input.send_keys(answer)
    submit_button = driver.find_element_by_xpath('//button[@type="submit"]')
    submit_button.click()

Summarize

This article describes how to use Python to write an automatic answering script. The principle of automatic question answering is to automatically complete the answering process by simulating human operation through the program. There are many ways to automatically answer questions in Python, such as using the PyAutoGUI library and the Selenium library. Although automatic answering is convenient and quick, there are some precautions that need to be paid attention to.

Please add a picture description

↓ ↓ ↓ Add the business card below to find me, directly get the source code and cases ↓ ↓ ↓

Guess you like

Origin blog.csdn.net/weixin_45841831/article/details/131215977