selenium+Chrome滑動(dòng)驗(yàn)證碼破解二(某某網(wǎng)站)
具體詳情見代碼,研究網(wǎng)站,隨便輸入手機(jī)號(hào)點(diǎn)擊獲取驗(yàn)證碼
在自己寫代碼前參考了一批博客,是把所有驗(yàn)證碼圖片截取所有驗(yàn)證碼圖片保存在本地,再對(duì)比,感覺方法不行,所以自己寫了個(gè)破解方法,通過js修改css直接抓取完整圖片,因?yàn)樯弦黄獙懥薆站,這里就不一一分析了,直接上代碼:
破解成功界面

完整代碼:
# -*- coding:utf-8 -*-
'''
研究網(wǎng)站: https://account.ch.com/NonRegistrations-Regist
滑塊驗(yàn)證碼也分兩種:
1.直接給缺口圖片,先滑動(dòng)到缺口找到完整驗(yàn)證碼圖片,對(duì)比有缺口的驗(yàn)證碼圖片,然后計(jì)算缺口坐標(biāo),再利用selenium移動(dòng)按鈕到指定位置
2.直接給原圖,缺口需要點(diǎn)擊出現(xiàn),直接保存原圖,然后對(duì)比
'''
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from lxml.html import etree
from PIL import Image
from time import sleep
import re, requests
from urllib.request import urlretrieve
from bs4 import BeautifulSoup
class SliderVerificationCode(object):
def __init__(self): # 初始化一些信息
self.left = 60 # 定義一個(gè)左邊的起點(diǎn) 缺口一般離圖片左側(cè)有一定的距離 有一個(gè)滑塊
self.url = 'https://account.ch.com/NonRegistrations-Regist'
self.chromedriverPath = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
self.driver = webdriver.Chrome(executable_path=self.chromedriverPath)
self.wait = WebDriverWait(self.driver, 20) # 設(shè)置等待時(shí)間20秒
self.phone = "18516544488"
def input_name_password(self): # 輸入手機(jī)號(hào)
self.driver.get(self.url)
self.driver.maximize_window()
self.inputphone = self.wait.until(EC.presence_of_element_located((By.NAME, 'phoneNumberInput')))
self.inputphone.send_keys(self.phone)
def click_login_button(self): # 點(diǎn)擊登錄按鈕,出現(xiàn)驗(yàn)證碼圖片
login_button = self.wait.until(EC.element_to_be_clickable((By.ID, 'getDynamicPwd')))
login_button.click()
sleep(1)
def get_geetest_image(self): # 獲取驗(yàn)證碼圖片
# print(self.driver.page_source)
gapimg = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_bg')))
sleep(2)
gapimg.screenshot(r'./captcha1.png')
# 通過js代碼修改標(biāo)簽樣式 顯示圖片2
js = 'var change = document.getElementsByClassName("geetest_canvas_fullbg");change[0].style = "display:block;"'
self.driver.execute_script(js)
sleep(2)
fullimg = self.wait.until(
EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_fullbg')))
fullimg.screenshot(r'./captcha2.png')
def is_similar(self, image1, image2, x, y):
'''判斷兩張圖片 各個(gè)位置的像素是否相同
#image1:帶缺口的圖片
:param image2: 不帶缺口的圖片
:param x: 位置x
:param y: 位置y
:return: (x,y)位置的像素是否相同
'''
# 獲取兩張圖片指定位置的像素點(diǎn)
pixel1 = image1.load()[x, y]
pixel2 = image2.load()[x, y]
# 設(shè)置一個(gè)閾值 允許有誤差
threshold = 60
# 彩色圖 每個(gè)位置的像素點(diǎn)有三個(gè)通道
if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs(
pixel1[2] - pixel2[2]) < threshold:
return True
else:
return False
def get_diff_location(self): # 獲取缺口圖起點(diǎn)
captcha1 = Image.open('captcha1.png')
captcha2 = Image.open('captcha2.png')
for x in range(self.left, captcha1.size[0]): # 從左到右 x方向
for y in range(captcha1.size[1]): # 從上到下 y方向
if not self.is_similar(captcha1, captcha2, x, y):
return x # 找到缺口的左側(cè)邊界 在x方向上的位置
def get_move_track(self, gap):
track = [] # 移動(dòng)軌跡
current = 0 # 當(dāng)前位移
# 減速閾值
mid = gap * 4 / 5 # 前4/5段加速 后1/5段減速
t = 0.2 # 計(jì)算間隔
v = 0 # 初速度
while current < gap:
if current < mid:
a = 3 # 加速度為+3
else:
a = -3 # 加速度為-3
v0 = v # 初速度v0
v = v0 + a * t # 當(dāng)前速度
move = v0 * t + 1 / 2 * a * t * t # 移動(dòng)距離
current += move # 當(dāng)前位移
track.append(round(move)) # 加入軌跡
return track
def move_slider(self, track):
slider = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.geetest_slider_button')))
ActionChains(self.driver).click_and_hold(slider).perform()
for x in track: # 只有水平方向有運(yùn)動(dòng) 按軌跡移動(dòng)
ActionChains(self.driver).move_by_offset(xoffset=x, yoffset=0).perform()
sleep(1)
ActionChains(self.driver).release().perform() # 松開鼠標(biāo)
def main(self):
self.input_name_password()
self.click_login_button()
self.get_geetest_image()
gap = self.get_diff_location() # 缺口左起點(diǎn)位置
gap = gap - 6 # 減去滑塊左側(cè)距離圖片左側(cè)在x方向上的距離 即為滑塊實(shí)際要移動(dòng)的距離
track = self.get_move_track(gap)
print("移動(dòng)軌跡", track)
self.move_slider(track)
if __name__ == "__main__":
springAutumn = SliderVerificationCode()
springAutumn.main()
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python OpenCV視頻截取并保存實(shí)現(xiàn)代碼
這篇文章主要介紹了Python OpenCV視頻截取并保存實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
python取數(shù)作為臨時(shí)極大值(極小值)的方法
今天小編就為大家分享一篇python取數(shù)作為臨時(shí)極大值(極小值)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python 使用requests模塊發(fā)送GET和POST請(qǐng)求的實(shí)現(xiàn)代碼
這篇文章主要介紹了Python 使用requests模塊發(fā)送GET和POST請(qǐng)求的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-09-09
在Mac OS上部署Nginx和FastCGI以及Flask框架的教程
這篇文章主要介紹了在Mac OS上部署Nginx和FastCGI以及Flask框架的教程,Flask是Python下一個(gè)極簡的web開放框架,需要的朋友可以參考下2015-05-05
13個(gè)Pandas實(shí)用技巧,助你提高開發(fā)效率
這篇文章主要介紹了13個(gè)Pandas實(shí)用技巧,幫助你提高python開發(fā)的效率,感興趣的朋友可以了解下2020-08-08
pytorch1.60 torch.nn在pycharm中無法自動(dòng)智能提示的解決
這篇文章主要介紹了pytorch1.60 torch.nn在pycharm中無法自動(dòng)智能提示的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02

