forked from xxcosmos/buy_pig_plan_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.py
147 lines (128 loc) · 4.54 KB
/
task.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
import time
from selenium.common.exceptions import NoSuchElementException, WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from config import target
def single_task(driver, url, wait):
"""
传入URL,处理单个页面
:param driver: driver
:param url: 要处理的url
:param wait: driver 等待
:return: 结果 success or error
"""
result = pre_handle(driver, url)
if result['status'] == 'error':
return result
result1 = comment_for_later_call(wait)
result2 = comment_for_immediate_call(wait)
if result1['status'] == 'success' or result2['status'] == 'success':
result = {'status': 'success'}
return result
return result2
def comment_for_immediate_call(wait):
"""
电话回拨系统(实时攻击)
:param wait: driver 等待
:return: 结果 success or error
"""
result = {'status': 'success'}
try:
phone_input = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "lxb-cb-input")))
phone_input.send_keys(target['phone'])
button = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "lxb-cb-input-btn")))
time.sleep(1)
button.click()
except Exception as e:
result['message'] = e
result['status'] = 'error'
return result
def comment_for_later_call(wait):
"""
留言系统实现(非实时攻击)
:param wait: driver 等待
:return: 结果 success or error
"""
result = {'status': 'success'}
try:
textarea = wait.until(EC.visibility_of_element_located((By.ID, "nb-nodeboard-set-content-js")))
textarea.send_keys(target['comment'])
input_list = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'nb-nodeboard-input')))
for text in input_list:
placeholder = str(text.get_attribute('placeholder'))
if '电话' in placeholder:
text.send_keys(target['phone'])
elif '姓' in placeholder:
text.send_keys(target['name'])
elif '邮' in placeholder:
text.send_keys(target['email'])
elif '地址' in placeholder:
text.send_keys(target['address'])
elif '必填' in placeholder:
text.send_keys(target['phone'])
time.sleep(0.5)
button = wait.until(EC.presence_of_element_located((By.ID, 'nb_nodeboard_send')))
button.click()
wait.until(EC.visibility_of_element_located((By.ID, 'nb_nodeboard_success')))
except Exception as e:
result['message'] = e
result['status'] = 'error'
return result
def pre_handle(driver, url):
"""
预处理 404、502、页面不存在 等问题
:param driver: driver
:param url: url
:return: 结果 success or error
"""
result = {'status': 'success'}
try:
driver.get(url)
except WebDriverException as e:
if 'NotFound' in e.msg:
result['message'] = Exception("404 Not Found")
result['status'] = 'error'
return result
else:
driver.execute_script("window.stop()")
except Exception as e:
result['message'] = e
result['status'] = 'error'
return result
try:
if driver.find_element_by_xpath("//*[contains(text(),'502')]") is not None:
raise Exception("502 Bad GatWay")
except NoSuchElementException as e:
pass
except Exception as e:
result['message'] = e
result['status'] = 'error'
return result
try:
if driver.find_element_by_xpath("//*[contains(text(),'无法进行访问')]") is not None:
raise Exception("502 Bad GatWay")
except NoSuchElementException as e:
pass
except Exception as e:
result['message'] = e
result['status'] = 'error'
return result
try:
if driver.find_element_by_xpath("//*[contains(text(),'无法访问')]") is not None:
raise Exception("502 Bad GatWay")
except NoSuchElementException as e:
pass
except Exception as e:
result['message'] = e
result['status'] = 'error'
return result
try:
if driver.find_element_by_xpath("//*[contains(text(),'404 Not Found')]") is not None:
raise Exception("404 Not Found")
except NoSuchElementException as e:
pass
except Exception as e:
result['message'] = e
result['status'] = 'error'
return result
return result