Skip to content

Commit 577ce44

Browse files
author
xirenbing
committed
add -n(--num) option
1 parent ba4a44c commit 577ce44

File tree

4 files changed

+83
-53
lines changed

4 files changed

+83
-53
lines changed

source_code/autocx.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from publicfunc import Color, getlogindata, getlogindata_phone, send_err, SYSTEM
1919
COLOR=Color()
2020

21-
def perform(mode,rate):
21+
def perform(mode,rate,noans_num):
2222
#处理账号信息
2323
print(COLOR.DISPLAY+'Welcome To Multi-Autocx!'+COLOR.END)
2424

@@ -33,7 +33,7 @@ def perform(mode,rate):
3333
print(' Sorry,no info')
3434
break
3535
# print(logindata)
36-
Popen('start cmd /k python login_courses.py '+logindata[0:-1]+' '+str(mode)+' '+str(rate), shell=True)
36+
Popen('start cmd /k python login_courses.py '+logindata[0:-1]+' '+str(mode)+' '+str(rate)+' '+str(noans_num), shell=True)
3737
sleep(2)
3838

3939
# 读取 机构账号 需要输入验证码 每次处理一个 按任意键后处理下一个
@@ -47,7 +47,7 @@ def perform(mode,rate):
4747
print(' Sorry,no info')
4848
break
4949
# print(logindata)
50-
Popen('start cmd /k python login_courses.py '+logindata[0:-1]+' '+str(mode)+' '+str(rate), shell=True)
50+
Popen('start cmd /k python login_courses.py '+logindata[0:-1]+' '+str(mode)+' '+str(rate)+' '+str(noans_num), shell=True)
5151
input(COLOR.OK+' please press any key to continue'+COLOR.END)
5252

5353
print(COLOR.DISPLAY+'Now you can exit this program! Good luck!'+COLOR.END)
@@ -56,22 +56,24 @@ def perform(mode,rate):
5656
def main():
5757
#参数处理
5858
try:
59-
opts,args=gnu_getopt(argv[1:],'-m:-r:-v-h',['mode=','rate=','version','help'])
59+
opts,args=gnu_getopt(argv[1:],'-m:-r:-n:-v-h',['mode=','rate=','num=','version','help'])
6060
except:
6161
print(COLOR.ERR+'Invalid args, Try -h or --help for more information'+COLOR.END)
6262
exit()
6363
#print(opts)
6464
#print(args)
6565
rate=1
6666
mode="single"
67+
noans_num=5
6768
opt_mode = ['single', 'fullauto', 'control']
6869
for opt_name,opt_value in opts:
6970
if opt_name in ('-h','--help'):
7071
print('''
7172
-m(--mode) single 单课程自动模式: 选择课程,自动完成该课程(默认启动参数)
7273
fullauto 全自动模式: 自动遍历全部课程,无需输入(除了机构登录方式下需要输入验证码)
7374
control 单课程控制模式: 选择课程并选择控制章节,自动完成[该课程第一个未完成章节,选定章节)范围内章节
74-
-r(--rate) [0.625,16] 全局倍速设置: 在选定模式的全局范围内开启该倍速
75+
-r(--rate) [0.625,16] 全局倍速设置: 在选定模式的全局范围内开启该倍速 默认值:1
76+
-n(--num) 0,1,2,3... 全局答题设置: 自动答题时,如果未找到答案的题目数量达到num,则暂时保存答案,不进行自动提交 默认值:5
7577
-h(--help) usage
7678
-v(--version) version
7779
''')
@@ -89,9 +91,17 @@ def main():
8991
if mode not in opt_mode:
9092
print(COLOR.ERR+'Invalid args, Try -h or --help for more information'+COLOR.END)
9193
exit()
94+
if opt_name in ('-n','--num'):
95+
try:
96+
noans_num=int(eval(opt_value))
97+
if noans_num<0:
98+
raise Exception
99+
except:
100+
print(COLOR.ERR+'Invalid args, Try -h or --help for more information'+COLOR.END)
101+
exit()
92102
#调用 执行 mode & rate
93-
print(COLOR.DISPLAY+'Set the mode: %s and the rate: %.2f'%(mode,rate)+COLOR.END)
94-
perform(opt_mode.index(mode),rate)
103+
print(COLOR.DISPLAY+'Set mode: %s\trate: %.2f\tnoans_num:%d'%(mode,rate,noans_num)+COLOR.END)
104+
perform(opt_mode.index(mode),rate,noans_num)
95105

96106
if __name__=='__main__':
97107
main()

source_code/login_courses.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from os import system as os_system,path as os_path,mkdir
3333
from singlecourse import SingleCourse as SC
3434
from publicfunc import Color, getlogindata, getlogindata_phone, send_err, SYSTEM
35+
from queryans import QueryAns
3536
COLOR = Color()
3637

3738
# 启动chrome
@@ -416,10 +417,14 @@ def _call_SC(self, courses_lt):
416417
logindata = getlogindata_phone()[0:2]
417418
mode = 0
418419
rate = 1
420+
noans_num = 5
419421
else:
420422
logindata = (argv[1]).split(',')
421423
mode = int(argv[2])
422424
rate = eval(argv[3])
425+
noans_num = eval(argv[4])
426+
QA=QueryAns()
427+
QA.noans_num=noans_num
423428
try:
424429
process = Login_courses_by_request(logindata, mode, rate)
425430
# process = Login_courses_by_chrome(logindata,mode,rate) #备用登录选项

source_code/queryans.py

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ class QueryAns(object):
1111
que_type = ['单选题', '多选题', '填空题', '判断题', '简答题', '名词解释题', '论述题', '计算题', '其他',
1212
'分录题', '资料题', '连线题', '', '排序题', '完形填空', '阅读理解', '', '', '口语题', '听力题']
1313
pd_opt = ['正确', '错误', '√', '×', '对', '错', '是', '否', 'T', 'F', 'ri', 'wr', 'true', 'false']
14+
noans_num = 5
15+
instance = None
16+
17+
def __new__(cls, *args, **kwargs):
18+
if cls.instance is None:
19+
cls.instance = super().__new__(cls)
20+
return cls.instance
1421

1522
def __init__(self, h5page='', *, question='', type='', course='', courseID=''):
1623
# 实例化方式:QueryAns(course,h5page)页面源码 操作:将处理源码内全部题目
1724
# QueryAns(course,**info)课程-题目-类型 将处理单个题目
1825
self.course = course
19-
self.courseID=courseID
26+
self.courseID = courseID
27+
self.no_ans_num = 0
2028
self.que_lt = []
2129
self.ans_ul = []
2230
self.que = ""
@@ -62,7 +70,7 @@ def work4page(self):
6270
sleep(3)
6371
# ans为0,未获取到答案
6472
if ans == 0:
65-
ans_order.append([1]) # 服务器异常,默认选1
73+
ans_order.append([0]) # 服务器异常,默认选1
6674
continue
6775

6876
ansopt = re.findall(r'<a.+?><?p?>?([\w\W]+?)<?/?p?>?</a>', self.ans_ul[i - 1]) # 当前题目 选项列表
@@ -89,39 +97,44 @@ def work4page(self):
8997
# else:
9098
# ans_order.append(1)
9199

92-
#f.write(str(ans_order[i - 1]) + '\n')
93-
#f.flush()
94-
#f.close()
95-
return ans_order # 返回一个列表,列表内的每一项是每个题目的答案列表
100+
if self.no_ans_num < QueryAns.noans_num:
101+
for index in range(0,len(ans_order)):
102+
if ans_order[index]==[0]:
103+
ans_order[index]==[1]
104+
return 2,ans_order
105+
else:
106+
return 1,ans_order # 返回一个列表,列表内的每一项是每个题目的答案列表
96107

97108
def _query_ans(self):
98-
res= self.GreasyFork_Group_API()
99-
if res==0 or res=='' or '暂未搜到' in res:
109+
res = self.GreasyFork_Group_API()
110+
if res == 0 or res == '' or '暂未搜到' in res:
100111
res = self.WangKeTiKu_API()
101-
if res=='' or res==0:
102-
res=self.SearchAns_GUI_API()
103-
if res=='':
104-
res=0
105-
if res!=0:
112+
if res == '' or res == 0:
113+
res = self.SearchAns_GUI_API()
114+
if res == '':
115+
res = 0
116+
if res != 0:
106117
send_que('courseID:'+self.courseID+' course:'+self.course + ' que:' + self.que + ' ans:' + str(res) + '\n')
118+
else:
119+
self.no_ans_num += 1
107120
return res
108121

109122
def SearchAns_GUI_API(self):
110-
#以原问题访问准确率更高___h5源码实例化的时候尽量不使用该方式
123+
# 以原问题访问准确率更高___h5源码实例化的时候尽量不使用该方式
111124
url = 'http://api.xmlm8.com/tk.php?t='+parse.quote(self.que_ori)
112125
try:
113126
ret_da = literal_eval(requestget(url).text)
114-
#print("que:"+ret_da['tm']+'\n'+"ans:"+ret_da['da'])
127+
# print("que:"+ret_da['tm']+'\n'+"ans:"+ret_da['da'])
115128
return ret_da['da']
116129
except:
117130
return 0
118131

119132
def GreasyFork_Group_API(self):
120-
#url = 'http://mooc.forestpolice.org/cx/0/' #WYN
133+
# url = 'http://mooc.forestpolice.org/cx/0/' #WYN
121134
url2 = 'http://voice.fafads.cn/xxt/api.php'
122135
url1 = 'http://129.204.175.209/cha_xin.php'
123136

124-
#def _prepare_query(index):
137+
# def _prepare_query(index):
125138
# data = {
126139
# 'courseId': '',
127140
# 'classId': '',
@@ -157,13 +170,13 @@ def GreasyFork_Group_API(self):
157170
'content': self.que
158171
}
159172

160-
def post_url(url,data):
173+
def post_url(url, data):
161174
headers = {
162175
'Content-type': 'application/x-www-form-urlencoded',
163176
}
164177
timeout = 30
165178
r = post(url, data, headers=headers, timeout=timeout)
166-
#print(r.text)
179+
# print(r.text)
167180
status = r.status_code # int
168181
# 200 且 code=1 响应成功
169182
# 200 且 code!=1 服务器繁忙
@@ -172,7 +185,7 @@ def post_url(url,data):
172185
if status == 200:
173186
try:
174187
res = literal_eval(r.text.strip(' \n'))
175-
#if res['code'] == 1:
188+
# if res['code'] == 1:
176189
# print(' 响应成功\n')
177190
# print(res['data'])
178191
try:
@@ -187,42 +200,42 @@ def post_url(url,data):
187200
# return 0
188201
# else:
189202
# print(' 服务器繁忙\n')
190-
#elif status == 403:
203+
# elif status == 403:
191204
# print(' 操作过于频繁\n')
192-
#else:
205+
# else:
193206
# print(' 服务器异常\n')
194207
return 0
195-
196-
res=post_url(url1,data1)
197-
if res==0:
198-
return post_url(url2,data2)
208+
209+
res = post_url(url1, data1)
210+
if res == 0:
211+
return post_url(url2, data2)
199212
else:
200213
return res
201214

202215
def WangKeTiKu_API(self):
203-
#print(self.que)
204-
url='http://www.wangketiku.com/getquestion.php?question='+self.que_ori
205-
headers={
216+
# print(self.que)
217+
url = 'http://www.wangketiku.com/getquestion.php?question='+self.que_ori
218+
headers = {
206219
'Host': 'www.wangketiku.com',
207-
'Referer':'http://www.wangketiku.com/?',
208-
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'
220+
'Referer': 'http://www.wangketiku.com/?',
221+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'
209222
}
210223
try:
211-
ret = literal_eval(requestget(url,headers=headers).text)['answer']
212-
#print(ret)
213-
ret=re.sub('[ \t\n\:]','',ret)
214-
ret=re.sub(r'题目(.+?)答案','',ret)
215-
index=ret.rfind('选项')
216-
if index!=-1:
217-
ret=ret[:index]
218-
ret_lt=ret.split('备选')
224+
ret = literal_eval(requestget(url, headers=headers).text)['answer']
225+
# print(ret)
226+
ret = re.sub('[ \t\n\:]', '', ret)
227+
ret = re.sub(r'题目(.+?)答案', '', ret)
228+
index = ret.rfind('选项')
229+
if index != -1:
230+
ret = ret[:index]
231+
ret_lt = ret.split('备选')
219232
del ret_lt[0]
220-
ans =""
233+
ans = ""
221234
for item in ret_lt:
222235
if '李恒道不会做这道题' in item or '暂无答案' in item:
223236
continue
224237
else:
225-
ans+=item+'#'
238+
ans += item+'#'
226239
return ans
227240
except:
228241
return 0
@@ -259,5 +272,6 @@ def test():
259272
QA = QueryAns('course_name', **infodic)
260273
print(QA.work())
261274

275+
262276
if __name__ == "__main__":
263277
test()

source_code/singlecourse.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def _ans_question(self):
622622
except:
623623
self.courseID=""
624624
QA=QueryAns(self.driver.page_source,course=self.course_name,courseID=self.courseID)
625-
ans_lt=QA.work()
625+
ans_flag,ans_lt=QA.work()
626626

627627
# print(ans_lt)
628628
# 开始答题
@@ -635,6 +635,8 @@ def _ans_question(self):
635635
try:
636636
for i in range(0, len(ans_lt)):
637637
for j in range(0, len(ans_lt[i])):
638+
if ans_lt[i][j]==0:
639+
continue
638640
try:
639641
# print(i,j,ans_lt[i][j])
640642
radio = self.driver.find_element_by_xpath(
@@ -653,8 +655,7 @@ def _ans_question(self):
653655
#log_fp.write(" 答题失败!" + '\n')
654656
sleep(200)
655657
return str(self._chapter) + '-' + str(self._section)
656-
657-
# sleep(60)
658+
658659
# 点击提交并确定,检测验证码
659660
# //*[@id="tempsave"]
660661
# //*[@id="ZyBottom"]/div/div[4]/div[4]/div[4]/div[5]/a[1]
@@ -663,9 +664,9 @@ def _ans_question(self):
663664
# //*[@id="ZyBottom"]/div[2]/a[2]/span
664665
try:
665666
bn = self.driver.find_element_by_xpath('//*[@id="ZyBottom"]/div' + '/div[4]' *
666-
(len(ans_lt) - 2) + '/div[5]/a[2]') # 多个题目
667+
(len(ans_lt) - 2) + '/div[5]/a['+str(ans_flag)+']') # 多个题目
667668
except:
668-
bn = self.driver.find_element_by_xpath('//*[@id="ZyBottom"]/div[2]/a[2]') # 只有一个题
669+
bn = self.driver.find_element_by_xpath('//*[@id="ZyBottom"]/div[2]/a['+str(ans_flag)+']') # 只有一个题
669670
# action_chains.move_to_element(bn)
670671
# bn.click()
671672
self.driver.execute_script("arguments[0].scrollIntoView();arguments[0].click();", bn)

0 commit comments

Comments
 (0)