python利用selenium庫識別點觸驗證碼
阿新 • • 發佈:2018-11-24
off 14. ati ESS class ott idt password until
利用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 fromselenium.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完成鬥魚找回密碼驗證
python利用selenium庫識別點觸驗證碼