-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
193 lines (169 loc) · 6.78 KB
/
main.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
"""
@Time : 2020-11-30 12:41
@Auth : DongZhou GU
@File :main.py
@IDE :PyCharm
"""
import asyncio
import re
from pyppeteer import launch
import time
from time import ctime, mktime
import yaml
import os
# 加载yaml
from pyppeteer.errors import ElementHandleError
def load_config(config_path):
with open(config_path, encoding='utf8') as f:
return yaml.load(f, Loader=yaml.FullLoader)
async def submit_choice(q, choice):
answers = await q.querySelectorAll('.ui-radio')
flag = False
for i, answer in enumerate(answers):
text = await (await answer.getProperty('textContent')).jsonValue()
if choice in text:
flag = True
break
if flag:
return await answers[i].click()
# 都没匹配到 随便选第一个
else:
return await answers[0].click()
async def submit_text(q, text): # 填空题
text_area = await q.querySelector('input')
await text_area.type(text)
async def submit_code(q, div_string, keyword): # 特殊人工验证
text_area = await q.querySelector('input')
i = div_string.rfind(keyword) + len(keyword)
j = max(div_string.rfind("("), div_string.rfind("("), -1)
res = []
res.append(re.compile(r'"(.*)"').findall(div_string[i:j]))
res.append(re.compile(r'“(.*)”').findall(div_string[i:j]))
res.append(re.compile(r'"(.*)”').findall(div_string[i:j]))
res.append(re.compile(r'“(.*)"').findall(div_string[i:j]))
flag = False
for i, r in enumerate(res):
if r:
flag = True
break
if flag:
await text_area.type(res[i][0])
else:
num = re.findall(r'[1-9]+\.?[0-9]*', div_string[i:j])
await text_area.type(num[0])
async def main_logic_text(q, div_string):
if "姓名" in div_string or "名字" in div_string:
await submit_text(q, info["name"])
elif "联系方式" in div_string or "手机" in div_string or "电话" in div_string:
await submit_text(q, info["phone"])
elif "学号" in div_string:
await submit_text(q, info["id"])
elif "身份证" in div_string:
await submit_text(q, info["card_id"])
elif "专业" in div_string:
await submit_text(q, info["major"])
elif "年级" in div_string:
await submit_text(q, info["grade"])
elif "性别" in div_string:
await submit_text(q, info["gender"])
elif "输入" in div_string:
await submit_code(q, div_string, "输入")
elif "填写" in div_string:
await submit_code(q, div_string, "填写")
else:
await submit_text(q, "1")
async def main_logic_choice(q, div_string):
if "专业" in div_string:
await submit_choice(q, info["major"])
elif "年级" in div_string:
await submit_choice(q, info["grade"])
elif "性别" in div_string:
await submit_choice(q, info["gender"])
else:
answers = await q.querySelectorAll('.ui-radio')
answers[0].click()
async def submit(page, questions, info):
for i, q in enumerate(questions):
element = await q.querySelector(f'#div{i + 1} > div.field-label')
div_string = await (await element.getProperty('textContent')).jsonValue()
# 是填空题
if await q.querySelectorAll('.ui-input-text'):
await main_logic_text(q, div_string)
# 是选择题
elif await q.querySelectorAll('.ui-radio'):
await main_logic_choice(q, div_string)
time.sleep(0.5)
submitb = await page.querySelector('#ctlNext')
await submitb.click()
time.sleep(1)
print(f'时间: {ctime()}==== {info["name"]}提交成功!不管是否爆红,请查看result.png截图进行验证!')
time.sleep(2)
# 登录成功截图
await page.screenshot({'path': './result.png', 'quality': 100, 'fullPage': True})
time.sleep(2)
# 主函数
async def main():
target_time = config['target_time']
browser = await launch({'headless': False,
'dumpio': True,
'ignoreDefaultArgs': ["--enable-automation"],
'args': [
# '--disable-extensions',
# '--disable-bundled-ppapi-flash',
# '--mute-audio',
# '--no-sandbox',
# '--disable-setuid-sandbox',
'--disable-gpu',
]})
# 打开新标签页
page = await browser.newPage()
await page.setUserAgent(useragent)
target_time = mktime(time.strptime(target_time, "%Y-%m-%d %H:%M:%S"))
global info
for i, info in enumerate(config['users']):
while (True):
if (time.time() > target_time):
await page.goto(url, {'timeout': 1000 * 60})
if (flag == "true"):
ok = await page.querySelector('#btnOk')
if (ok != None):
await asyncio.wait([
ok.click(),
page.waitForNavigation({'timeout': 1000 * 60}),
])
# await asyncio.wait([
# page.querySelectorAll('.field'), page.waitForNavigation({'timeout': 50000}), ])
await asyncio.wait([
page.querySelectorAll('.field')])
questions = await page.querySelectorAll('.field')
# print(questions)
if (len(questions) != 0): break
else:
questions = await page.querySelectorAll('.field')
if (len(questions) != 0): break
else:
print(f'时间: {ctime()}====等待中')
time.sleep(1)
print(f'时间: {ctime()}===={info["name"]}开始提交')
await submit(page, questions, info)
print(f'时间: {ctime()}====任务完成')
# 关闭浏览器
# await browser.close()
# 运行入口
if __name__ == '__main__':
if os.path.exists("./result.png"):
os.remove("./result.png")
config = load_config("setting_config.yaml")
flag = config['need_weixin']
ind = config['wjx_id'].rfind('/')
ind2 = config['wjx_id'].rfind('.aspx')
if (flag == "true"):
url = config['url']
index = url.find('.aspx&response_type')
index2 = url.find("state=sojump")
url = url[:index - 7] + config['wjx_id'][ind + 1:ind2] + url[index:index2 + 12] + "&connect_redirect=1" + url[
index2 + 12:]
if (flag == "false"):
url = "https://www.wjx.cn/vm" + config['wjx_id'][ind:]
useragent = config['user-agent']
asyncio.get_event_loop().run_until_complete(main())