-
Notifications
You must be signed in to change notification settings - Fork 33
/
search_question.py
213 lines (204 loc) · 8.52 KB
/
search_question.py
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
import argparse
import re
import sys
import threading
import signal
import json
try:
import tailer
import requests
from selenium import webdriver
from bs4 import BeautifulSoup
import pyfiglet
except Exception:
print("请先阅读帮助文档README.md")
print("试试:pip3 install -r related.txt")
parser=argparse.ArgumentParser(description="冲顶大会/百万赢家/头脑王者/UC疯狂夺金:抓包获取题目并搜索答案。")
parser.add_argument("-b","--brand",dest='brand',help="选择APP,1:冲顶大会;2:百万赢家;3:头脑王者;4:UC疯狂夺金",choices=[1,2,3,4],type=int)
args=parser.parse_args()
CHROME = webdriver.Chrome()
def handlesignal(signum,frame):
print('\033[93m'+"\n程序已停止")
CHROME.close()
sys.exit(0)
signal.signal(signal.SIGINT,handlesignal)
class GetAnswer(object):
def __init__(self,brand,raw_data):
self.brand=brand
self.raw_data=raw_data
def clean_cddh(self):
try:
self.question = self.raw_data.split(':')[2].split(',')[0].replace('"', '').replace('?', '').replace('?', '').split('.')[
-1].strip()
options_raw_list = self.raw_data.split(':')[5].split(',')
self.option_list = []
for i in range(3):
tmp = options_raw_list[i].replace('"', '').replace(']', '').replace('[', '').replace('\\', '')
self.option_list.append(tmp)
except Exception as e:
print("发生错误了:", e)
def clean_bwyj(self):
try:
self.question = self.raw_data['data']['msg']['answer']['doing']['doing']['title'].replace('?','').replace('“','').replace('”','').strip()
self.question=self.question.replace('以下','')
options_a = self.raw_data['data']['msg']['answer']['doing']['doing']['answer']['A']['value']
options_b = self.raw_data['data']['msg']['answer']['doing']['doing']['answer']['B']['value']
self.option_list = [options_a, options_b]
#百万赢家非正常选择题,有时只有两个选项
try:
options_c = self.raw_data['data']['msg']['answer']['doing']['doing']['answer']['C']['value']
self.option_list.append(options_c)
except Exception:
pass
except Exception:
pass
def clean_tnwz(self):
try:
self.question=self.raw_data['data']['quiz'].replace('?','').replace('“','').replace('”','').strip()
self.question=self.question.replace('以下','')
self.option_list=self.raw_data['data']['options']
except Exception:
pass
def clean_fkdj(self):
try:
self.question=self.raw_data['data']['args'][0]['body']['qc'].replace('?','').replace('“','').replace('”','').strip()
self.ac=self.raw_data['data']['args'][0]['body']['as']
self.option_list=[]
for i in self.ac:
tmp=i['ac'].strip()
self.option_list.append(tmp)
except Exception:
pass
def getAnswer_chrome(self):
global CHROME
url_base = 'https://www.baidu.com/s?wd='
# url_base='https://www.sogou.com/web?query='
url = url_base + self.question
CHROME.get(url)
def getAnswer_index_count(self,question,option_list):
url_base = 'http://www.baidu.com/s'
result_dict = {}
try:
for option in option_list:
r = requests.get(url_base, params={'wd': question})
result = r.text.count(option)
#print('\033[95m'+"首页数量统计结果:",option, ":", result)
result_dict[result] = option
if '不' in question:
print('\033[91m' + "根据首页统计,答案可能是:", result_dict[min(result_dict)])
else:
print('\033[91m' + "根据首页统计,答案可能是:", result_dict[max(result_dict)])
print('-'*20)
except Exception as e:
print("发生错误了:", e)
def getAnswer_all_count(self,question,option_list):
url_base = 'http://www.baidu.com/s'
result_dict = {}
try:
for option in option_list:
query = question + '+' + option
r = requests.get(url_base, params={'wd': query})
soup = BeautifulSoup(r.text, 'lxml')
result = soup.find('div', {'class': 'nums'}).getText()
result = result.replace(',', '')
result = int(re.findall('\d+', result)[0])
#print('\033[95m'+"全部数量统计结果:",option, ':', result)
result_dict[result] = option
if '不' in question:
print('\033[91m' + "根据全部统计,答案可能是:", result_dict[min(result_dict)])
else:
print('\033[91m' + "根据全部统计,答案可能是:", result_dict[max(result_dict)])
print('-'*20)
except Exception as e:
print("发生错误了:", e)
def run(self):
try:
if self.brand == 1:
self.clean_cddh()
elif self.brand == 2:
self.clean_bwyj()
elif self.brand ==3:
self.clean_tnwz()
elif self.brand == 4:
self.clean_fkdj()
print('\033[92m' + "题目:", self.question)
print('\033[93m' + "选项:", self.option_list)
print('\033[95m')
t1 = threading.Thread(target=self.getAnswer_chrome)
t2 = threading.Thread(target=self.getAnswer_index_count, args=(self.question, self.option_list))
t3 = threading.Thread(target=self.getAnswer_all_count, args=(self.question, self.option_list))
t_list = [t1, t2, t3]
for t in t_list:
t.start()
for t in t_list:
t.join()
print('*' * 50)
except Exception as e:
print("发生错误了:", e)
def info():
fig=pyfiglet.Figlet('slant')
HEADER=fig.renderText('F_ANSWER')
WRITER='https://github.com/vanpersiexp'
print('\033[95m'+HEADER)
print('\033[95m'+WRITER.center(50))
print('\033[95m'+"使用Ctrl+C停止.".center(50))
def main():
if args.brand:
brand_2_old=''
brand_4_old=''
for raw in tailer.follow(open('/tmp/raw_data.txt','r')):
if args.brand == 1:
if 'showQuestion' in raw:
game=GetAnswer(args.brand,raw)
game.run()
elif args.brand == 2:
try:
raw=raw.split('(')[-1].split(')')[0]
raw_json=json.loads(raw)
raw_question=raw_json['data']['msg']['answer']['doing']['doing']['title']
raw_question_showanswer=raw_json['data']['msg']['answer']['doing']['doing']['show_answer']
if not raw_question_showanswer:
if raw_question != brand_2_old:
game=GetAnswer(args.brand,raw_json)
game.run()
brand_2_old = raw_question
except Exception:
continue
elif args.brand == 3:
raw_json=json.loads(raw)
try:
raw_json['data']['quiz']
game=GetAnswer(args.brand,raw_json)
game.run()
except Exception:
continue
elif args.brand == 4:
try:
if 'question' in raw:
if raw != brand_4_old:
raw_json=json.loads(raw)
game=GetAnswer(args.brand,raw_json)
game.run()
brand_4_old=raw
except Exception:
continue
else:
print("python3 search_question -h")
print("请查看帮助文档,目前仅支持4个APP的抓包获取题目。")
sys.exit(1)
else:
print("python3 search_question -h")
print("请查看帮助文档,需要选择对应的APP。")
print("例如,选择冲顶大会:python3 search_question -b 1")
sys.exit(1)
if __name__=='__main__':
try:
info()
main()
except Exception as e:
print("发生错误了:",e)
finally:
try:
CHROME.close()
except Exception:
pass