python利用selenium库识别点触验证码

利用selenium库和超级鹰识别点触验证码(学习于静谧大大的书,想自己整理一下思路)

一、超级鹰注册:超级鹰入口

  1、首先注册一个超级鹰账号,然后在超级鹰免费测试地方可以关注公众号,领取1000积分,基本上就够学习使用了。如果想一直用可以用,可以充值,不是很贵。

  2、下载超级鹰的python库代码。代码

  3、然后有测试案例,自己可以试着跑一跑代码。

二、使用selenium库来识别点触式验证码:

  1、首先是找一个使用点触式二维码的网站:(这个真的是比较难找了,由于静谧大大书上的网站被封了,我找了好久,才找到斗鱼的找回密码是用的点触式验证码,将就着用吧)。

  2、开始操作:

    (1)首先声明一个类,定义属性:

      

 1 '''
 2     func:斗鱼找回密码,点触式二维码
 3     author:monty
 4     date:2018/11/24
 5 '''
 6 from chaojiying import Chaojiying_Client
 7 from selenium import webdriver
 8 from selenium.webdriver.support.wait import WebDriverWait
 9 from selenium.webdriver.support import expected_conditions as EC
10 from selenium.webdriver.common.by import By
11 import time
12 from PIL import Image
13 from io import BytesIO
14 from selenium.webdriver import ActionChains
15 
16 #填写自己的斗鱼注册手机号
17 tel=
18 #超级鹰的类型码
19 kind=9004
20 class CrackGeetest():
21     def __init__(self):
22         self.url='https://www.douyu.com/member/findpassword/findByPhone'
23         self.browser=webdriver.Chrome()
24         self.browser.get(self.url)
25         self.wait=WebDriverWait(self.browser,20)
26         self.tel=tel
27         self.chaojiying=Chaojiying_Client('超级鹰账号', '超级鹰密码',kind)

 

    (2)填写输入框信息:

1     def set_tel(self):
2         '''
3         填写telephonenumber
4         :return:
5         '''
6         #获取输入框
7         input=self.wait.until(EC.presence_of_element_located((By.ID,'reg_userphone')))
8         input.clear()
9         input.send_keys(self.tel)

 

   (3)获得初始的机器验证按钮:

    

1     def get_geetest_button(self):
2         '''
3             获取初始验证按钮
4         :return:
5         '''
6         button=self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'geetest_radar_tip')))
7         return button

 

    (4)获取点触验证码图片:

  

 1     def get_image(self):
 2         '''
 3             获取验证码图片
 4         :return: 图片对象
 5         '''
 6         image=self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,'geetest_widget')))
 7         return image
 8 
 9     def get_position(self):
10         #获取图片的位置信息
11         image=self.get_image()
12         time.sleep(2)
13         location=image.location
14         size=image.size
15         top,bottom,left,right=location['y'],location['y']+size['height']-55,location['x'],location['x']+size['width']
16         return (top,bottom,left,right)
17 
18     def get_screenshot(self):
19         '''
20         获取整个屏幕截屏
21         :return:
22         '''
23         screenshot=self.browser.get_screenshot_as_png()
24         screenshot=Image.open(BytesIO(screenshot))
25         return screenshot
26 
27     def get_touclick_image(self, name='captcha.png'):
28         """
29         获取验证码图片
30         :return: 图片对象
31         """
32         top, bottom, left, right = self.get_position()
33         print('验证码位置', top, bottom, left, right)
34         screenshot = self.get_screenshot()
35         captcha = screenshot.crop((left, top, right, bottom))
36         captcha.save(name)
37         return captcha
38     def __del__(self):
39         self.browser.close()

    (5)利用超级鹰获得需要点触的位置:

  

1 #获取验证码截图
2     image=cg.get_touclick_image()
3     bytes_array=BytesIO()
4     image.save(bytes_array,format='PNG')
5     #识别验证码
6     result=cg.chaojiying.PostPic(bytes_array.getvalue(),kind)

    (6)根据位置来点触验证码:

    

 1     def getPoint(self,result):
 2         '''
 3         获取每个坐标点
 4         :param result:
 5         :return: 返回坐标位置
 6         '''
 7         groups=result.get('pic_str').split('|')
 8         locations=[[int(number) for number in group.split(',')] for group in groups]
 9         return locations
10 
11     def touch_click_words(self,locations):
12         '''
13         点击坐标
14         :param locations:
15         :return:
16         '''
17 
18         for location in locations:
19             print(location)
20             ActionChains(self.browser).move_to_element_with_offset(self.get_image(), location[0],
21                                                                    location[1]).click().perform()
22             time.sleep(1)

    (7)最后点击提交按钮:

    

1     def submit(self):
2         submit=self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'geetest_commit')))
3         submit.click()
4         time.sleep(5)
5         button=self.wait.until(EC.element_to_be_clickable((By.ID,'submit-fp-ph')))
6         button.click()

  3、基本流程就是这样,爬虫就是为了模拟用户的操作,跟黑客没什么关系,一点也不高大上!!!

  附github代码:selenium完成斗鱼找回密码验证

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/monty12/p/10012126.html