selenium 实现点触验证码识别
YY点触验证码识别案例
这个网站在第一次请求网站的时候,用selenium定位不到验证码,最后发现是iframe标签的问题
我们需要再次请求iframe里面的链接,打开新的网页之后就可以定位操作了。
1 # !/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 from chaojiying import Chaojiying 5 import random 6 import time 7 from PIL import Image 8 from io import BytesIO 9 from selenium import webdriver 10 from selenium.webdriver.support.ui import WebDriverWait 11 from selenium.webdriver import ActionChains 12 from selenium.webdriver.common.by import By 13 from selenium.webdriver.support import expected_conditions as ec 14 15 CHAOJIYING_NAME = '******' # 超级鹰账号 16 CHAOJIYING_PWD = '********' # 超级鹰密码 17 CHAOJIYING_ID = 894613 # 软件ID 18 CHAOJIYING_KIND = 9004 # 验证码类型 19 20 21 class YYVerification(object): 22 """ 23 此类用于YY的验证码识别,可以应用到类似的验证码识别上,这种验证码类型是 24 点击类验证码.这里我们是对接超级鹰平台。 25 """ 26 def __init__(self): 27 self.url = 'https://aq.yy.com/p/reg/account.do?appid=&url=&fromadv=udbclsd_r' 28 self.driver = webdriver.Chrome() 29 self.chaojiying = Chaojiying(CHAOJIYING_NAME, CHAOJIYING_PWD, CHAOJIYING_ID) 30 31 def __del__(self): 32 self.driver.close() 33 34 def screen_shot(self): 35 """ 36 网页截屏 37 :return: bool值 38 """ 39 # self.driver.maximize_window() 40 time.sleep(2) 41 # # 用于网页的向下滚动 42 # js = 'var q=document.documentElement.scrollTop=300' 43 # self.driver.execute_script(js) 44 # time.sleep(1) 45 self.driver.save_screenshot('yy.png') 46 return True 47 48 def i_url(self): 49 """ 50 实现对iframe标签的url请求,并定位新网页 51 :return: 定位对象 52 """ 53 i = self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe') 54 url_1 = i.get_attribute('src') 55 print(url_1) 56 self.driver.get(url_1) 57 wait = WebDriverWait(self.driver, 20) 58 element = wait.until(ec.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]'))) 59 return element 60 61 def shear_location(self): 62 """ 63 计算对截屏剪切的坐标,注意这里不同电脑可能会修改start_x到end_y 64 后边添加的数是根据作者的电脑优化的 65 :return: 返回坐标以及网页剪切定位的对象 66 """ 67 time.sleep(random.random() + 1) 68 print('正在获取div') 69 div = self.i_url() 70 start_x = div.location['x'] + 25 71 start_y = div.location['y'] + 65 72 end_x = div.location['x'] + 350 + 25 73 end_y = div.location['y'] + 218 + 65 74 result = (start_x, start_y, end_x, end_y) 75 print(result) 76 return result, div 77 78 @staticmethod 79 def shear_image(axis): 80 im = Image.open('yy.png') 81 new_image = im.crop(axis) 82 new_image.save('new_img.png') 83 return new_image 84 85 def upload_picture(self, img): 86 """ 87 上传图片(Byte),返回点击的坐标。这里对返回的坐标进行了处理, 88 处理过程可以看成 shear_location 函数的坐标数据逆处理过程 89 :param img: 上传的图片 90 :return: 点击坐标 91 """ 92 image = img 93 byte_array = BytesIO() 94 image.save(byte_array, format('PNG')) 95 # 提交图片 96 result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND) 97 print(result) 98 if '无可用题分' in result['err_str']: 99 print('题分已经不足请充值') 100 raise ValueError 101 pic_str = result['pic_str'] 102 pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')] 103 for i in pic_list: 104 i[0] = int(int(i[0])*(282/354)) 105 i[1] = int(int(i[1])*(219/274)) 106 print(pic_list) 107 return pic_list 108 109 def click_now(self, coordinates, axis, element): 110 """ 111 这里如果验证失败会进行回调,实现验证码的自动验证。 112 :param coordinates: 点击坐标 113 :param axis: 剪切坐标 114 :param element: 剪切位置的定位 115 :return: 返回点击的成功信息 116 """ 117 print('点击开始') 118 print(coordinates) 119 for location in coordinates: 120 ActionChains(self.driver).move_to_element_with_offset(element, location[0], location[1]).click().perform() 121 time.sleep(random.random() + 1.8) 122 submission = self.driver.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]') 123 submission.click() 124 time.sleep(1) 125 if 'pw_submit_disabled' in submission.get_attribute('class'): 126 return '点击成功' 127 else: 128 time.sleep(2) 129 print('正在重新识别新的验证码') 130 self.revalidation(axis, element) 131 return '点击成功' 132 133 def revalidation(self, axis, element): 134 """ 135 网页跳转之后,执行验证的主程序 136 :param axis: 剪切坐标 137 :param element: 剪切位置的定位对象 138 :return: 验证成果 139 """ 140 self.screen_shot() 141 if not self.screen_shot(): 142 return '截图失败' 143 time.sleep(1) 144 # 剪切图片 145 new_image = self.shear_image(axis) 146 new_image.show() 147 # 上传图片并返回点击坐标 148 click_coordinates = self.upload_picture(new_image) 149 # 点击验证码 150 print(self.click_now(click_coordinates, axis, element)) 151 return '点击验证结束' 152 153 def main(self): 154 self.driver.get(self.url) 155 wait = WebDriverWait(self.driver, 20) 156 wait.until(ec.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div/iframe'))) 157 axis, element = self.shear_location() 158 self.revalidation(axis, element) 159 160 161 if __name__ == '__main__': 162 yy = YYVerification() 163 yy.main()
清澈的爱,只为中国