@@ -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+
262276if __name__ == "__main__" :
263277 test ()
0 commit comments