AI自动刷题

由于时效问题,该文某些代码、技术可能已经过期,请注意!!!本文最后更新于:10 小时前

AI自动刷题

以一个python网站为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import time
import re
from playwright.sync_api import sync_playwright
from bs4 import BeautifulSoup
import os
import requests
import json
from openai import OpenAI
import openai


def doubaoAI(user_content):
# 使用豆包需要自行申请
client = OpenAI(
# 此为默认路径,您可根据业务所在地域进行配置
base_url="xxxx",
# 从环境变量中获取您的 API Key。此为默认方式,您可根据需要进行修改
api_key='xxx',
)

response = client.chat.completions.create(
# 指定您创建的方舟推理接入点 ID,此处已帮您修改为您的推理接入点 ID
model="doubao-seed-1-6-250615",
messages=[
{
"role": "user",
"content": user_content
}
],
)
return response.choices[0].message.content


def pollAI(user_content):
url = "https://text.pollinations.ai/openai"
availiable_models = ["openai-large", 'openai', 'openai-fast', 'qwen-coder', "grok", 'searchgpt']
for model in availiable_models:
try:
payload = {
"model": model, # Or "mistral", etc.
"messages": [
{"role": "user", "content": user_content}
],
"seed": 101,
"temperature":0.1,
}
headers = {
"Content-Type": "application/json"
}

response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return response.json()['choices'][0]['message']['content']
except:
continue


def ai_answer(user_content):
res = pollAI(user_content)
return res


def process_div():
# 获取最新页面内容
html_content = page.content()
soup = BeautifulSoup(html_content, "html.parser")
div = soup.find("div", class_="quiz-content")

quiz = div.find('div', class_='quiz-question')
quiz_text = quiz.text
print(quiz_text)

answer_box = div.find('div', class_='quiz-right-box')
labels = answer_box.find_all('label')
quiz_ls = {}

input_type = "radio" # 默认是单选

quiz_options = ''
for i, quiz_opt in enumerate(labels):
quiz_input = quiz_opt.find('input')
if i == 0:
input_type = quiz_input.get('type') # 从第一个选项判断是 radio 还是 checkbox

option_text = quiz_opt.find('p').text
quiz_ls[i] = quiz_input.get('value')
quiz_options += f'{i}:{option_text}\n\n'



user_content = f'''
你是Python专家,下面是一个python问题,帮我解答一下。

问题是:
{quiz_text}
选项是:
{quiz_options}

只输出答案的编号即可, 如果有多个答案就以英文逗号分隔, 不要输出多余的内容。
'''
res = ai_answer(user_content)
res = res.split(',')
# print(res)
for r in res:
input_value = quiz_ls.get(int(r), quiz_ls[0])
# print(f'选择:{r} {input_value}')
selector = f'input[type="{input_type}"][value="{input_value}"]'
page.click(selector)
time.sleep(1)


def fake_submit(target_url):
page.goto(target_url)
page.wait_for_load_state("networkidle")

button_start = page.locator('button', has_text=re.compile("Start|Proceed", re.IGNORECASE))
button_start.click()
page.wait_for_load_state("networkidle")
time.sleep(0.5)
button_accept = page.locator('button').filter(has_text='I accept')
if button_accept.count() == 1:
button_accept.click()
page.wait_for_load_state("networkidle")
time.sleep(0.5)
button_start = page.locator('button', has_text=re.compile("Start|Proceed", re.IGNORECASE))
button_start.click()

page.wait_for_selector('div.quiz-content') # 确保题目区域加载

while True:
time.sleep(3)
process_div() # 每次处理都重新获取 soup & div

button_next = page.locator('button:has-text("Next")')
try:
if button_next.is_visible():
button_next.click()
page.wait_for_selector('div.quiz-content') # 等下一题加载出来
time.sleep(1) # 稍微等一下防止卡住
else:
break
except TimeoutError:
break

# 先绑定 dialog 事件,准备接受弹窗
page.on("dialog", lambda dialog: dialog.accept())

# 点击第一次 Submit
submit_button = page.locator('button:has-text("Submit")')
submit_button.click()

# 等待第二个 Submit 出现并点击
page.wait_for_selector('button:has-text("Submit")', timeout=5000)
confirm_submit = page.locator('button:has-text("Submit")')
confirm_submit.click()

# 等待模态框加载
page.wait_for_selector('#submit-quiz-modal', timeout=5000)

# 点击模态框中的 “Yes”
page.locator('#submit-quiz-modal button:has-text("Yes")').click()
time.sleep(5)


# 启动 Playwright
with sync_playwright() as p:
# 启动浏览器
browser = p.chromium.launch(headless=True) # 设置为 False 可观察浏览器操作
context = browser.new_context()
page = context.new_page()

# Step 1: 登录
login_url = "https://edube.org/login"
page.goto(login_url)

# 填写用户名和密码
page.fill('input[name="_username"]', 'xxx') # 替换为你的用户名
page.fill('input[name="_password"]', 'xxx') # 替换为你的密码

# 提交登录表单
page.click('button[type="submit"]') # 确保选择器是登录按钮的准确选择器

# 等待导航到登录后的页面
page.wait_for_load_state('networkidle')

# url = "https://edube.org/quiz/pe-2/pe2-module-1-quiz-1" # 替换为目标页面 URL
quiz_urls = [
"https://edube.org/quiz/pe-2/pe2-module-1-quiz-1",
"https://edube.org/quiz/pe-2/pe2-module-1-test-1",
"https://edube.org/quiz/pe-2/pe2-module-2-quiz-1",
"https://edube.org/quiz/pe-2/pe2-module-2-test-1",
"https://edube.org/quiz/pe-2/pe2-module-3-quiz-1",
"https://edube.org/quiz/pe-2/pe2-module-3-test-1",
"https://edube.org/quiz/pe-2/pe2-module-4-quiz-1",
"https://edube.org/quiz/pe-2/pe2-module-4-test-1",
'https://edube.org/quiz/pe-2/pe2-summary-test-1'
]
for url in quiz_urls:
fake_submit(url)
time.sleep(3)

# 关闭浏览器
browser.close()

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!