-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
336 lines (291 loc) · 8.96 KB
/
main.py
File metadata and controls
336 lines (291 loc) · 8.96 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# conda activate selenium
from selenium import webdriver
from time import sleep
import re
import json
import sys
import os
from docx import Document
d = webdriver.Chrome()
pros = {}
class Problem:
def __init__(self, problem, selections, correct):
self.problem = problem
self.selections = selections
self.correct = correct
@classmethod
def from_dic(self, dic):
return self(dic["problem"], dic["selections"], dic["correct"])
def __eq__(self, other):
return (
self.problem == other.problem
and self.selections == other.selections
and self.correct == other.correct
)
def get_ans(self):
return self.correct.split("、")
def get_ans_by_current_selections(self, selections):
ans = self.get_ans_content()
ret = ""
for k, v in selections.items():
if v in ans:
ret += k
if len(ret) == len(self.correct):
return ret
else:
return self.correct
def get_ans_content(self):
ret = []
for e in self.get_ans():
ret.append(self.selections[e])
return ret
def to_readable(self):
return {
"problem": self.problem,
"selections": self.selections,
"correct": self.correct,
}
def regex_str(str):
return str
def waiting_for_url(url):
while d.current_url != url:
pass
def update_problem(problem, selections, correct):
p = Problem(problem, selections, correct)
if not problem in pros:
if problem != "无题干数据":
pros[problem] = p
print("获取到")
print(p.to_readable())
else:
pros[problem] = [p]
print("特殊更新")
print("获取到")
print(p.to_readable())
else:
print("存在相同的,进行对比")
if problem == "无题干数据":
print("特殊更新")
tag = 1
for e in pros[problem]:
if e == p:
print("相同,无需更新")
tag = 0
if tag:
print("不同更新为")
pros[problem].append(p)
print(p.to_readable())
else:
if pros[problem] == p:
print("相同,无需更新")
else:
print("不同更新为")
print(p.to_readable())
pros[problem] = p
def download_memory(url):
d.get(url)
sleep(1)
while True:
content = regex_str(
"\n".join(
d.find_elements_by_tag_name("main")[0].text.split("\n")[2::][:-3:]
)
).split("\n")
problem = content[0]
selections = {}
for e in content[1:-1:]:
if e[0].isalpha():
selections[e[0]] = e[1::]
else:
problem += "\n"
problem += e
if content[-1].startswith("正确答案"):
correct = content[-1][5::]
else:
print(content)
print("[Error]这个题目有点问题,没有记录")
sleep(5)
return
update_problem(problem, selections, correct)
if not d.find_elements_by_tag_name("button")[1].is_enabled():
break
d.find_elements_by_tag_name("button")[1].click()
sleep(1)
def save_to_text():
f = open(file, "w")
ret = {}
for k, v in pros.items():
if k != "无题干数据":
ret[k] = v.to_readable()
else:
ret[k] = []
for e in v:
ret[k].append(e.to_readable())
f.write(json.dumps(ret))
f.close()
def read_from_file():
try:
f = open(file, "r")
ret = f.read()
for k, v in json.loads(ret).items():
if k != "无题干数据":
pros[k] = Problem.from_dic(v)
else:
pros[k] = []
for e in v:
pros[k].append(Problem.from_dic(e))
except:
print(f"读取{file}可能遇到了问题,不如删掉试试吧")
def make_result():
while 1:
content = regex_str(
"\n".join(
d.find_elements_by_tag_name("main")[0].text.split("\n")[2::][:-3:]
)
).split("\n")[:-1:]
problem = content[0]
selections = {}
for e in content[1::]:
if e[0].isalpha():
selections[e[0]] = e[1::]
else:
problem += "\n"
problem += e
ans = []
if problem in pros:
if problem != "无题干数据":
p = pros[problem]
ans = p.get_ans_by_current_selections(selections)
print(f"找到“{problem}”\n对应答案{ans}")
else:
for e in pros[problem]:
if e.selections == selections:
ans = e.get_ans()
for e in ans:
for i in d.find_elements_by_tag_name("li"):
if i.text.startswith(e):
try:
i.click()
except:
d.execute_script(
"window.scrollTo(0, document.body.scrollHeight)"
)
i.click()
if len(ans) == 0:
print(f"没找到“{problem}”")
print("请自行选择答案,不用按下下一题")
os.system("pause>nul")
sleep(1)
if not d.find_elements_by_tag_name("button")[2].is_enabled():
break
d.find_elements_by_tag_name("button")[2].click()
sleep(1)
d.find_elements_by_tag_name("button")[0].click()
sleep(1)
d.find_elements_by_tag_name("button")[2].click()
def get_len():
ret = len(pros.items())
if "无题干数据" in pros:
ret += len(pros["无题干数据"]) - 1
return ret
def add_problem(doc):
doc.add_heading("行测题库 by 恒星", 0)
para = doc.add_paragraph()
i = 1
def func(p: Problem):
nonlocal i
problem = p.problem
selections = p.selections
correct = p.correct
para.add_run(f"{i}.{problem}").bold = True
para.add_run().add_break()
for k, v in selections.items():
para.add_run(f" {k}{v}")
para.add_run().add_break()
para.add_run(f"正确答案{correct}").italic = True
para.add_run().add_break()
i += 1
return func
file = "data.txt"
exam = "https://exam.yooc.me/group/9755188/exams"
reviews = ["https://exam.yooc.me/group/9755112/exam/490501/review/134408164"]
def login():
read_from_file()
d.get(exam)
waiting_for_url("https://www.yooc.me/mobile/yooc")
d.get(exam)
def train():
"""
for e in reviews:
download_memory(e)
"""
print("请进入考试界面")
os.system("pause>nul")
sleep(1)
make_result()
sleep(10)
d.find_elements_by_tag_name("button")[-1].click()
sleep(1)
download_memory(d.current_url)
print(f"本次更新完后题目数量为{get_len()}")
save_to_text()
# d.close()
def train_by_file_2025_5_6():
def split_by_pattern(text):
"""
按照"A.字符串"的格式分割字符串
"""
# 使用正向肯定预查匹配字母+点号的模式作为分割点
pattern = r"(?=[A-Za-z]\.)"
# 进行分割并过滤可能的空字符串
result = [s for s in re.split(pattern, text) if s]
return result
read_from_file()
f = open("./2025.5.6.txt", encoding="utf-8")
data = f.read()
f.close()
pattern = r"(\d+、)\s*([\s\S]*?)(A\..*?)(正确答案:[\s\S]*?)(?=\d+、|$)"
question_blocks = re.findall(pattern, data, re.DOTALL)
for question in question_blocks:
problem = question[1].replace("\n", "")
correct = question[3].replace("\n", "")[5::]
selections = {}
for per in split_by_pattern(question[2].replace("\n", "")):
try:
if per == "":
continue
temp = per.split(".")
selections[temp[0]] = f".{temp[1]}"
except:
print(f"出现问题{question}")
raise
update_problem(problem, selections, correct)
save_to_text()
def word():
read_from_file()
doc = Document()
writer = add_problem(doc)
for k, v in pros.items():
if k != "无题干数据":
writer(v)
else:
for e in v:
writer(e)
doc.save("result.docx")
def main():
print("请进入考试后按下任意键继续")
os.system("pause>nul")
make_result()
print("按下任意键退出程序")
os.system("pause>nul")
if __name__ == "__main__":
if sys.argv[1] == "train":
login()
train()
elif sys.argv[1] == "word":
word()
else:
login()
main()
d.close()
# the logging finished