识别滑动验证码 :param driver: selenium驱动 :return:
(driver)
| 124 | |
| 125 | |
| 126 | def recognize_code(driver): |
| 127 | """ |
| 128 | 识别滑动验证码 |
| 129 | :param driver: selenium驱动 |
| 130 | :return: |
| 131 | """ |
| 132 | |
| 133 | bs = BeautifulSoup(driver.page_source,'lxml') |
| 134 | # 找到背景图片和缺口图片的div |
| 135 | bg_div = bs.find_all(class_='gt_cut_bg_slice') |
| 136 | fullbg_div = bs.find_all(class_='gt_cut_fullbg_slice') |
| 137 | |
| 138 | # 获取缺口背景图片url |
| 139 | bg_url = re.findall('background-image:\surl\("(.*?)"\)',bg_div[0].get('style')) |
| 140 | # 获取背景图片url |
| 141 | fullbg_url = re.findall('background-image:\surl\("(.*?)"\)',fullbg_div[0].get('style')) |
| 142 | |
| 143 | # 存放每个合成缺口背景图片的位置 |
| 144 | bg_location_list = [] |
| 145 | # 存放每个合成背景图片的位置 |
| 146 | fullbg_location_list = [] |
| 147 | |
| 148 | for bg in bg_div: |
| 149 | location = {} |
| 150 | location['x'] = int(re.findall('background-position:\s(.*?)px\s(.*?)px;', bg.get('style'))[0][0]) |
| 151 | location['y'] = int(re.findall('background-position:\s(.*?)px\s(.*?)px;', bg.get('style'))[0][1]) |
| 152 | bg_location_list.append(location) |
| 153 | |
| 154 | for fullbg in fullbg_div: |
| 155 | location = {} |
| 156 | location['x'] = int(re.findall('background-position:\s(.*?)px\s(.*?)px;', fullbg.get('style'))[0][0]) |
| 157 | location['y'] = int(re.findall('background-position:\s(.*?)px\s(.*?)px;', fullbg.get('style'))[0][1]) |
| 158 | fullbg_location_list.append(location) |
| 159 | |
| 160 | print(bg_location_list) |
| 161 | print(fullbg_location_list) |
| 162 | |
| 163 | # 将图片格式存为 jpg 格式 |
| 164 | bg_url = bg_url[0].replace('webp', 'jpg') |
| 165 | fullbg_url = fullbg_url[0].replace('webp', 'jpg') |
| 166 | # print(bg_url) |
| 167 | # print(fullbg_url) |
| 168 | |
| 169 | # 下载图片 |
| 170 | bg_image = requests.get(bg_url).content |
| 171 | fullbg_image = requests.get(fullbg_url).content |
| 172 | print('完成图片下载') |
| 173 | |
| 174 | # 写入图片 |
| 175 | bg_image_file = BytesIO(bg_image) |
| 176 | fullbg_image_file = BytesIO(fullbg_image) |
| 177 | |
| 178 | # 合成图片 |
| 179 | bg_Image = mergy_Image(bg_image_file, bg_location_list) |
| 180 | fullbg_Image = mergy_Image(fullbg_image_file, fullbg_location_list) |
| 181 | # bg_Image.show() |
| 182 | # fullbg_Image.show() |
| 183 |
no test coverage detected