利用selenium库和超级鹰识别点触验证码(学习于静谧大大的书,想自己整理一下思路)
一、超级鹰注册:超级鹰入口
1、首先注册一个超级鹰账号,然后在超级鹰免费测试地方可以关注公众号,领取1000积分,基本上就够学习使用了。如果想一直用可以用,可以充值,不是很贵。
2、下载超级鹰的python库代码。代码
3、然后有测试案例,自己可以试着跑一跑代码。
二、使用selenium库来识别点触式验证码:
1、首先是找一个使用点触式二维码的网站:(这个真的是比较难找了,由于静谧大大书上的网站被封了,我找了好久,才找到斗鱼的找回密码是用的点触式验证码,将就着用吧)。
2、开始操作:
(1)首先声明一个类,定义属性:
1 ‘‘‘
2 func:斗鱼找回密码,点触式二维码3 author:monty4 date:2018/11/245 ‘‘‘
6 from chaojiying importChaojiying_Client7 from selenium importwebdriver8 from selenium.webdriver.support.wait importWebDriverWait9 from selenium.webdriver.support importexpected_conditions as EC10 from selenium.webdriver.common.by importBy11 importtime12 from PIL importImage13 from io importBytesIO14 from selenium.webdriver importActionChains15
16 #填写自己的斗鱼注册手机号
17 tel=
18 #超级鹰的类型码
19 kind=9004
20 classCrackGeetest():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=tel27 self.chaojiying=Chaojiying_Client(‘超级鹰账号‘, ‘超级鹰密码‘,kind)
(2)填写输入框信息:
1 defset_tel(self):2 ‘‘‘
3 填写telephonenumber4 :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 defget_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 defget_image(self):2 ‘‘‘
3 获取验证码图片4 :return: 图片对象5 ‘‘‘
6 image=self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,‘geetest_widget‘)))7 returnimage8
9 defget_position(self):10 #获取图片的位置信息
11 image=self.get_image()12 time.sleep(2)13 location=image.location14 size=image.size15 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 defget_screenshot(self):19 ‘‘‘
20 获取整个屏幕截屏21 :return:22 ‘‘‘
23 screenshot=self.browser.get_screenshot_as_png()24 screenshot=Image.open(BytesIO(screenshot))25 returnscreenshot26
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 returncaptcha38 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 defgetPoint(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 ingroups]9 returnlocations10
11 deftouch_click_words(self,locations):12 ‘‘‘
13 点击坐标14 :param locations:15 :return:16 ‘‘‘
17
18 for location inlocations: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 defsubmit(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、基本流程就是这样,爬虫就是为了模拟用户的操作,跟黑客没什么关系,一点也不高大上!!!