1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3
4 from chaojiying importChaojiying5 importrandom6 importtime7 from PIL importImage8 from io importBytesIO9 from selenium importwebdriver10 from selenium.webdriver.support.ui importWebDriverWait11 from selenium.webdriver importActionChains12 from selenium.webdriver.common.by importBy13 from selenium.webdriver.support importexpected_conditions as ec14
15 CHAOJIYING_NAME = '******' # 超级鹰账号
16 CHAOJIYING_PWD = '********' # 超级鹰密码
17 CHAOJIYING_ID = 894613 # 软件ID
18 CHAOJIYING_KIND = 9004 # 验证码类型
19
20
21 classYYVerification(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 defscreen_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 returnTrue47
48 defi_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 returnelement60
61 defshear_location(self):62 """
63 计算对截屏剪切的坐标,注意这里不同电脑可能会修改start_x到end_y64 后边添加的数是根据作者的电脑优化的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 returnresult, div77
78 @staticmethod79 defshear_image(axis):80 im = Image.open('yy.png')81 new_image =im.crop(axis)82 new_image.save('new_img.png')83 returnnew_image84
85 defupload_picture(self, img):86 """
87 上传图片(Byte),返回点击的坐标。这里对返回的坐标进行了处理,88 处理过程可以看成 shear_location 函数的坐标数据逆处理过程89 :param img: 上传的图片90 :return: 点击坐标91 """
92 image =img93 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 raiseValueError101 pic_str = result['pic_str']102 pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]103 for i inpic_list:104 i[0] = int(int(i[0])*(282/354))105 i[1] = int(int(i[1])*(219/274))106 print(pic_list)107 returnpic_list108
109 defclick_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 incoordinates: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 defrevalidation(self, axis, element):134 """
135 网页跳转之后,执行验证的主程序136 :param axis: 剪切坐标137 :param element: 剪切位置的定位对象138 :return: 验证成果139 """
140 self.screen_shot()141 if notself.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 defmain(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()