1+ # -*-coding:utf-8 -*-
2+ import requests
3+ from lxml import etree
4+ from bs4 import BeautifulSoup
5+ import urllib
6+ import time , re , types , os
7+
8+
9+ """
10+ 代码写的匆忙,本来想再重构下,完善好注释再发,但是比较忙,想想算了,所以自行完善吧!
11+
12+ 作者: Jack Cui
13+ Website:http://cuijiahua.com
14+ 注: 本软件仅用于学习交流,请勿用于任何商业用途!
15+ """
16+
17+ class BaiWan ():
18+ def __init__ (self ):
19+ # 百度知道搜索接口
20+ self .baidu = 'http://zhidao.baidu.com/search?'
21+ # 百万英雄及接口,每个人的接口都不一样,里面包含的手机信息,因此不公布,请自行抓包,有疑问欢迎留言:http://cuijiahua.com/liuyan.html
22+ self .api = 'https://api-spe-ttl.ixigua.com/xxxxxxx={}' .format (int (time .time ()* 1000 ))
23+
24+ # 获取答案并解析问题
25+ def get_question (self ):
26+ to = True
27+ while to :
28+ list_dir = os .listdir ('./' )
29+ if 'question.txt' not in list_dir :
30+ fw = open ('question.txt' , 'w' )
31+ fw .write ('百万英雄尚未出题请稍后!' )
32+ fw .close ()
33+ go = True
34+ while go :
35+ req = requests .get (self .api , verify = False )
36+ req .encoding = 'utf-8'
37+ html = req .text
38+
39+ print (html )
40+ if '*' in html :
41+ question_start = html .index ('*' )
42+ try :
43+
44+ question_end = html .index ('?' )
45+ except :
46+ question_end = html .index ('?' )
47+ question = html [question_start :question_end ][2 :]
48+ if question != None :
49+ fr = open ('question.txt' , 'r' )
50+ text = fr .readline ()
51+ fr .close ()
52+ if text != question :
53+ print (question )
54+ go = False
55+ with open ('question.txt' , 'w' ) as f :
56+ f .write (question )
57+ else :
58+ time .sleep (1 )
59+ else :
60+ to = False
61+ else :
62+ to = False
63+
64+ temp = re .findall (r'[\u4e00-\u9fa5a-zA-Z0-9\+\-\*/]' , html [question_end + 1 :])
65+ b_index = []
66+ print (temp )
67+
68+ for index , each in enumerate (temp ):
69+ if each == 'B' :
70+ b_index .append (index )
71+ elif each == 'P' and (len (temp ) - index ) <= 3 :
72+ b_index .append (index )
73+ break
74+
75+ if len (b_index ) == 4 :
76+ a = '' .join (temp [b_index [0 ] + 1 :b_index [1 ]])
77+ b = '' .join (temp [b_index [1 ] + 1 :b_index [2 ]])
78+ c = '' .join (temp [b_index [2 ] + 1 :b_index [3 ]])
79+ alternative_answers = [a ,b ,c ]
80+
81+ if '下列' in question :
82+ question = a + ' ' + b + ' ' + c + ' ' + question .replace ('下列' , '' )
83+ elif '以下' in question :
84+ question = a + ' ' + b + ' ' + c + ' ' + question .replace ('以下' , '' )
85+ else :
86+ alternative_answers = []
87+ # 根据问题和备选答案搜索答案
88+ self .search (question , alternative_answers )
89+ time .sleep (1 )
90+
91+ def search (self , question , alternative_answers ):
92+ print (question )
93+ print (alternative_answers )
94+ infos = {"word" :question }
95+ # 调用百度接口
96+ url = self .baidu + 'lm=0&rn=10&pn=0&fr=search&ie=gbk&' + urllib .parse .urlencode (infos , encoding = 'GB2312' )
97+ print (url )
98+ headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36' ,
99+ }
100+ sess = requests .Session ()
101+ req = sess .get (url = url , headers = headers , verify = False )
102+ req .encoding = 'gbk'
103+ # print(req.text)
104+ bf = BeautifulSoup (req .text , 'lxml' )
105+ answers = bf .find_all ('dd' ,class_ = 'dd answer' )
106+ for answer in answers :
107+ print (answer .text )
108+
109+ # 推荐答案
110+ recommend = ''
111+ if alternative_answers != []:
112+ best = []
113+ print ('\n ' )
114+ for answer in answers :
115+ # print(answer.text)
116+ for each_answer in alternative_answers :
117+ if each_answer in answer .text :
118+ best .append (each_answer )
119+ print (each_answer ,end = ' ' )
120+ # print(answer.text)
121+ print ('\n ' )
122+ break
123+ statistics = {}
124+ for each in best :
125+ if each not in statistics .keys ():
126+ statistics [each ] = 1
127+ else :
128+ statistics [each ] += 1
129+ errors = ['没有' , '不是' , '不对' , '不正确' ,'错误' ,'不包括' ,'不包含' ,'不在' ,'错' ]
130+ error_list = list (map (lambda x : x in question , errors ))
131+ print (error_list )
132+ if sum (error_list ) >= 1 :
133+ for each_answer in alternative_answers :
134+ if each_answer not in statistics .items ():
135+ recommend = each_answer
136+ print ('推荐答案:' , recommend )
137+ break
138+ elif statistics != {}:
139+ recommend = sorted (statistics .items (), key = lambda e :e [1 ], reverse = True )[0 ][0 ]
140+ print ('推荐答案:' , recommend )
141+
142+ # 写入文件
143+ with open ('file.txt' , 'w' ) as f :
144+ f .write ('问题:' + question )
145+ f .write ('\n ' )
146+ f .write ('*' * 50 )
147+ f .write ('\n ' )
148+ if alternative_answers != []:
149+ f .write ('选项:' )
150+ for i in range (len (alternative_answers )):
151+ f .write (alternative_answers [i ])
152+ f .write (' ' )
153+ f .write ('\n ' )
154+ f .write ('*' * 50 )
155+ f .write ('\n ' )
156+ f .write ('参考答案:\n ' )
157+ for answer in answers :
158+ f .write (answer .text )
159+ f .write ('\n ' )
160+ f .write ('*' * 50 )
161+ f .write ('\n ' )
162+ if recommend != '' :
163+ f .write ('最终答案请自行斟酌!\t ' )
164+ f .write ('推荐答案:' + sorted (statistics .items (), key = lambda e :e [1 ], reverse = True )[0 ][0 ])
165+
166+
167+ if __name__ == '__main__' :
168+ bw = BaiWan ()
169+ bw .get_question ()
0 commit comments