python驗(yàn)證碼識(shí)別教程之滑動(dòng)驗(yàn)證碼
前言
上篇文章記錄了2種分割驗(yàn)證碼的方法,此外還有一種叫做”滴水算法”(Drop Fall Algorithm)的方法,但本人智商原因看這個(gè)算法看的云里霧里的,所以今天記錄滑動(dòng)驗(yàn)證碼的處理吧。網(wǎng)上據(jù)說(shuō)有大神已經(jīng)破解了滑動(dòng)驗(yàn)證碼的算法,可以不使用selenium來(lái)破解,但本人能力不足還是使用笨方法吧。
基礎(chǔ)原理很簡(jiǎn)單,首先點(diǎn)擊驗(yàn)證碼按鈕后的圖片是滑動(dòng)后的完整結(jié)果,點(diǎn)擊一下滑塊后會(huì)出現(xiàn)拼圖,對(duì)這2個(gè)分別截圖后比較像素值來(lái)找出滑動(dòng)距離,并結(jié)合selenium來(lái)實(shí)現(xiàn)拖拽效果。
至于selenium怎么安裝就不說(shuō)了,滑動(dòng)驗(yàn)證碼的一個(gè)難點(diǎn)就是要模擬人的拖拽行為,移動(dòng)快了不行,慢了也不行。
這里以國(guó)家企業(yè)公示網(wǎng)站為例:
# -*- coding: utf-8 -*- import time import random from io import BytesIO from PIL import Image from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class Slide(object): """滑動(dòng)驗(yàn)證碼破解""" def __init__(self, target): self.target = target # 要搜索的公司名稱 self.driver = webdriver.Chrome() self.wait = WebDriverWait(self.driver, 10) def crop(self, left, top, right, bottom, pic_name): """截屏并裁剪""" ss = Image.open(BytesIO(self.driver.get_screenshot_as_png())) cp = ss.crop((left, top, right, bottom)) # 注意這里順序 cp.save(pic_name) return cp def calc_move(self, pic1, pic2): """根據(jù)閾值計(jì)算移動(dòng)距離""" pix1 = pic1.load() pix2 = pic2.load() threshold = 200 move = 0 # 因?yàn)榛瑝K都從左向右滑動(dòng),而碎片本身寬度為60所以從60開始遍歷 for i in range(60, pic1.size[0]): flag = False for j in range(pic1.size[1]): r = abs(pix1[i, j][0] - pix2[i, j][0]) g = abs(pix1[i, j][1] - pix2[i, j][1]) b = abs(pix1[i, j][2] - pix2[i, j][2]) # if r > threshold and g > threshold and b > threshold: # 方法1:分別判斷rgb大于閾值 # flag = True # break if r + g + b > threshold: # 方法2:判斷rgb總和跟閾值比較,效果比1好 為什么呢?? flag = True break if flag: move = i break return move def path1(self, distance): """繪制移動(dòng)路徑方法1,構(gòu)造一個(gè)等比數(shù)列""" q = 0.4 # 測(cè)試后發(fā)現(xiàn)0.4效果最佳 n = 10 # 最多移動(dòng)幾次 a1 = ((1 - q) * distance) / (1 - q**n) result = [] for o in range(1, n + 1): an = a1 * q**(o - 1) if an < 0.1: # 小于移動(dòng)閾值的就不要了 break t = random.uniform(0, 0.5) # 測(cè)試后0.5秒的間隔成功率最高 result.append([an, 0, t]) return result def path2(self, distance): """繪制移動(dòng)路徑方法2,模擬物理加速、減速運(yùn)動(dòng),效果比1好""" result = [] current = 0 # 減速閾值 mid = distance * 4 / 5 # 計(jì)算間隔 t = 0.2 # 初速度 v = 0 while current < (distance - 10): if current < mid: # 加速度為正2 a = 2 else: # 加速度為負(fù)3 a = -3 # 初速度v0 v0 = v # 當(dāng)前速度v = v0 + at v = v0 + a * t # 移動(dòng)距離x = v0t + 1/2 * a * t^2 move = v0 * t + 0.5 * a * t * t # 當(dāng)前位移 current += move # 加入軌跡 result.append([round(move), 0, random.uniform(0, 0.5)]) return result def run(self): self.driver.get("http://www.gsxt.gov.cn/index") input_box = self.driver.find_element_by_id('keyword') input_box.send_keys(self.target) search_btn = self.driver.find_element_by_id('btn_query') time.sleep(3) # 注意這里等一下再點(diǎn),否則會(huì)出現(xiàn)卡死現(xiàn)象 search_btn.click() # 等待驗(yàn)證碼彈出 bg_pic = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, "gt_cut_fullbg"))) # html中坐標(biāo)原點(diǎn)是左上角,右為x軸正方向,下為y軸正方向 # 輸出的x為正就是此元素距離屏幕左側(cè)距離 # 輸出的y為正就是此元素距離屏幕上側(cè)距離 # 所以我們需要截圖的四個(gè)距離如下: top, bottom, left, right = ( bg_pic.location['y'], bg_pic.location['y'] + bg_pic.size['height'], bg_pic.location['x'], bg_pic.location['x'] + bg_pic.size['width']) time.sleep(1) cp1 = self.crop(left, top, right, bottom, '1.png') # 獲取滑塊按鈕并點(diǎn)擊一下 slide = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, "gt_slider_knob"))) slide.click() time.sleep(3) # 等3秒報(bào)錯(cuò)信息消失 TODO 這里應(yīng)該可以改進(jìn) cp2 = self.crop(left, top, right, bottom, '2.png') move = self.calc_move(cp1, cp2) result = self.path1(move) # result = self.path2(move) # 拖動(dòng)滑塊 ActionChains(self.driver).click_and_hold(slide).perform() for x in result: ActionChains(self.driver).move_by_offset(xoffset=x[0],yoffset=x[1]).perform() # ActionChains(driver).move_to_element_with_offset(to_element=slide,xoffset=x[0],yoffset=x[1]).perform() time.sleep(x[-1]) # 如果使用方法1則需要sleep time.sleep(0.5) ActionChains(self.driver).release(slide).perform() # 釋放按鈕 time.sleep(0.8) element = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, "gt_info_text"))) ans = element.text if u"通過" in ans: # 這里也需要等一下才能獲取到具體的鏈接 element = self.wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, "search_list_item"))) for o in self.driver.find_elements_by_xpath(u"http://a[@target='_blank']"): print(o.get_attribute("href")) self.driver.quit() else: print("識(shí)別失敗") self.driver.quit() if __name__ == '__main__': s = Slide('中國(guó)平安') s.run()
代碼中注釋很詳細(xì)就不多說(shuō)了,如果運(yùn)行時(shí)候提示
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
則需要到 https://sites.google.com/a/chromium.org/chromedriver/home 下載驅(qū)動(dòng)后解壓到/usr/local/bin目錄即可。
使用服務(wù)器運(yùn)行時(shí)使用phantomjs替換chrome,另外失敗的時(shí)候可以進(jìn)行判斷自動(dòng)重試,有興趣的小伙伴可以自己補(bǔ)充完善。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Python實(shí)現(xiàn)新版正方系統(tǒng)滑動(dòng)驗(yàn)證碼識(shí)別
- python自動(dòng)化操作之動(dòng)態(tài)驗(yàn)證碼、滑動(dòng)驗(yàn)證碼的降噪和識(shí)別
- 使用python實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼功能
- Python 200行代碼實(shí)現(xiàn)一個(gè)滑動(dòng)驗(yàn)證碼過程詳解
- Python破解極驗(yàn)滑動(dòng)驗(yàn)證碼詳細(xì)步驟
- 用Python爬蟲破解滑動(dòng)驗(yàn)證碼的案例解析
- 基于python實(shí)現(xiàn)破解滑動(dòng)驗(yàn)證碼過程解析
- python破解bilibili滑動(dòng)驗(yàn)證碼登錄功能
- python自動(dòng)化測(cè)試之破解滑動(dòng)驗(yàn)證碼
相關(guān)文章
Python中關(guān)鍵字nonlocal和global的聲明與解析
這篇文章主要給大家介紹了關(guān)于Python中關(guān)鍵字nonlocal和global的聲明與解析的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03Scrapy爬蟲框架集成selenium及全面詳細(xì)講解
這篇文章主要為大家介紹了Scrapy集成selenium,以及scarpy爬蟲框架全面講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04python如何繪制極坐標(biāo)輪廓圖contourf
這篇文章主要介紹了python如何繪制極坐標(biāo)輪廓圖contourf問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08