| 12
 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
 214
 215
 216
 
 | import timeimport random
 import requests
 
 from cv2 import cv2 as cv
 from selenium.webdriver import Chrome
 from selenium.webdriver.common.by import By
 from selenium.webdriver import ActionChains
 from selenium.webdriver.support.ui import WebDriverWait
 from selenium.webdriver.support import expected_conditions as EC
 
 
 driver = Chrome()
 
 
 class Tx():
 
 def __init__(self):
 
 
 self.url = 'http://dld.qzapp.z.qq.com/qpet/cgi-bin/phonepk?zapp_uin=&sid=b+BqyBVjL/uOqlmjH2/Kp0/GqwABVg/196790a110201==&channel=0&g_ut=1&cmd=index'
 
 self.username = '123456345'
 
 self.password = "abcdxdk"
 
 
 self.wait = WebDriverWait(driver, 10)
 
 def login(self):
 '''
 实现网站登陆
 '''
 
 
 driver.get(self.url)
 
 
 账号 = self.wait.until(EC.presence_of_element_located((By.ID, 'u')))
 账号.send_keys(self.username)
 
 
 密码 = self.wait.until(EC.presence_of_element_located((By.ID, 'p')))
 密码.send_keys(self.password)
 
 
 self.wait.until(EC.presence_of_element_located((By.ID, 'go'))).click()
 time.sleep(5)
 
 
 
 iframe = self.wait.until(
 EC.frame_to_be_available_and_switch_to_it((By.ID, 'tcaptcha_iframe')))
 if iframe:
 self.tx_code()
 
 def tx_code(self):
 '''
 滑动滑块
 '''
 
 
 bk_block = self.wait.until(EC.presence_of_element_located(
 (By.ID, 'slideBg'))).get_attribute('src')
 
 if self.save_img(bk_block):
 dex = self.get_pos()
 track_list = self.get_track(dex)
 time.sleep(0.5)
 
 
 slid_ing = self.wait.until(EC.presence_of_element_located(
 (By.ID, 'tcaptcha_drag_thumb')))
 
 
 ActionChains(driver).click_and_hold(
 on_element=slid_ing).perform()
 
 time.sleep(0.2)
 print('轨迹', track_list)
 
 for track in track_list:
 
 ActionChains(driver).move_by_offset(
 xoffset=track, yoffset=0).perform()
 time.sleep(1)
 
 
 ActionChains(driver).release(
 on_element=slid_ing).perform()
 time.sleep(1)
 
 
 return True
 else:
 print('缺口图片捕获失败')
 return False
 
 @staticmethod
 def save_img(bk_block):
 '''
 将验证码缺口图片保存到当前目录下
 不要用中文路径, 会报错
 '''
 
 try:
 
 img = requests.get(bk_block)
 time.sleep(0.25)
 
 
 with open('./tx.jpeg', 'wb') as f:
 f.write(img.content)
 
 return True
 except:
 return False
 
 @staticmethod
 def get_pos():
 '''
 识别缺口
 注意:网页上显示的图片为缩放图片, 缩放 50 % 所以识别坐标需要 0.5
 contours                轮廓图像, 类型list
 hierarchy               每条轮廓对应的属性, 属于 numpy.ndarray 类
 cv.RETR_EXTERNAL        表示只检测外轮廓
 cv.CHAIN_APPROX_SIMPLE  压缩水平、垂直、对角线方向的元素, 只保留该方向的终点坐标, 例如一个矩形轮廓只需4个点来保存轮廓信息
 '''
 
 
 image = cv.imread('./tx.jpeg')
 
 
 blurred = cv.GaussianBlur(image, (5, 5), 0)
 
 
 canny = cv.Canny(blurred, 200, 400)
 
 contours, _ = cv.findContours(
 canny, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
 for _, contour in enumerate(contours):
 
 m = cv.moments(contour)
 if m['m00'] == 0:
 cx = _ = 0
 else:
 cx, _ = m['m10'] / m['m00'], m['m01'] / m['m00']
 
 
 if 6000 < cv.contourArea(contour) < 8000 and 370 < cv.arcLength(contour, True) < 390:
 if cx < 400:
 continue
 
 
 x, y, w, h = cv.boundingRect(contour)
 
 
 cv.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
 
 print('【缺口识别】 {x}px'.format(x=x/2))
 return x/2
 return 0
 
 @staticmethod
 def get_track(distance):
 '''
 模拟轨迹
 '''
 
 
 distance -= 26
 
 v = 0
 
 t = 0.2
 
 tracks = []
 
 current = 0
 
 mid = distance * 7 / 8
 
 
 distance += 10
 
 while current < distance:
 if current < mid:
 
 a = random.randint(2, 4)
 else:
 a = -random.randint(3, 5)
 
 
 v0 = v
 
 s = v0 * t + 0.5 * a * (t ** 2)
 
 current += s
 
 tracks.append(round(s))
 
 
 v = v0 + a * t
 
 
 for _ in range(4):
 tracks.append(-random.randint(2, 3))
 for _ in range(4):
 tracks.append(-random.randint(1, 3))
 return tracks
 
 
 if __name__ == '__main__':
 
 Tx = Tx()
 Tx.login()
 
 |