展示一波sao操作~来用Python实现自动答题
案例源码以及所用到的资料点击文末名片获取
导入模块
import requests
import re
import csv
import concurrent.futures
import parsel
def AnswerID():
f = open('答案ID.txt', encoding='utf-8')
answer_id_list = [answer_id for answer_id in f.read().split(',')]
return answer_id_list
def GetResponse(html_url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
html_data = requests.get(url=html_url, headers=headers).text
return html_data
def GetContent(AnswerID):
AnswerUrl = f'https://***/Post/{AnswerID}.htm'
源码、教程、解答+VX:xiaoyuanllsll
html_data = GetResponse(html_url=AnswerUrl)
selector = parsel.Selector(html_data)
problem = selector.css('#question a::text').get()
answer = selector.css('#question h1 b::text').getall()
if len(answer) == 0:
answer = selector.css('#question h1 u::text').get()
AnswerDict = {
'Problem': problem,
'Answer': answer,
}
return AnswerDict
def main(page):
AnswerDict = GetContent(AnswerID=page)
csv_writer.writerow(AnswerDict)
print(AnswerDict)
if __name__ == '__main__':
# GetContent(AnswerID='8502e')
answer_id_list = AnswerID()
f = open('科目四题库.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['Problem', 'Answer'])
csv_writer.writeheader()
exe = concurrent.futures.ThreadPoolExecutor(max_workers=7)
for answer_id in answer_id_list:
exe.submit(main, answer_id)
exe.shutdown()
导入模块
from selenium import webdriver
import pandas as pd
import re
df = pd.read_csv('题库.csv')
key = []
value = []
获取每一道题目
for i in df['Problem']: # 问题作为键
把题目添加到列表里面
key.append(i)
获取每一道题目答案
for j in df['Answer']: # 问题作为值
把答案添加到列表里面
value.append(j)
把两个列表合并成字典
dic = dict(zip(key, value))
打开浏览器
Chrome 浏览器 谷歌
driver = webdriver.Chrome()
访问网站
driver.get('https://www.jsyks.com/kms-fzks')
driver.maximize_window()
答题操作:
题库 <获取出来了>
读取csv文件, 把题目内容以及答案读取出来
print(dic)
for page in range(50):
Questions = driver.find_element_by_css_selector('#ExamTit').text
# \d+ 匹配数字 获取题目
Question = re.findall('\d+. (.*)', Questions)[0]
answer = dic[Question]
# 多选题, 答案肯定大于1
if len(answer) == 1:
# 判断答案内容 选择选项
if answer == '对':
# 定位对元素, 进行点击操作
driver.find_element_by_css_selector('#TX1 #btnDa11').click()
elif answer == '错':
driver.find_element_by_css_selector('#TX1 #btnDa12').click()
elif answer == 'A':
driver.find_element_by_css_selector('#TX2 #btnDa21').click()
elif answer == 'B':
driver.find_element_by_css_selector('#TX2 #btnDa22').click()
elif answer == 'C':
driver.find_element_by_css_selector('#TX2 #btnDa23').click()
elif answer == 'D':
源码、教程、解答+VX:xiaoyuanllsll
driver.find_element_by_css_selector('#TX2 #btnDa24').click()
# 点击下一题
driver.find_element_by_css_selector('#actArea #btnNext2').click()
elif len(answer) > 1:
# 多选
for x in answer:
if x == 'A':
driver.find_element_by_css_selector('#TX2 #btnDa21').click()
elif x == 'B':
driver.find_element_by_css_selector('#TX2 #btnDa22').click()
elif x == 'C':
driver.find_element_by_css_selector('#TX2 #btnDa23').click()
elif x == 'D':
driver.find_element_by_css_selector('#TX2 #btnDa24').click()
driver.find_element_by_css_selector('#actArea #btnNext2').click()
driver.find_element_by_css_selector('#actArea #btnNext3').click()
driver.find_element_by_css_selector('#Tip_Msg span input:nth-child(1)').click()
感兴趣的宝子记得点击名片获取代码,也可以私信我~