bilibili的滑動驗證碼圖片比較好玩,和前一篇不大同樣。python
採用canvas方法,分析發現只找到一個圖片,不過,能夠經過設置display截圖方式得到2張圖(完整圖片,帶缺口的圖片),取得圖片後接下來的方式和前一篇同樣,偏移位置參數存在差別,須要自行調試。完整代碼以下web


#!/usr/bin/env python # encoding: utf-8 #@author: jack #@contact: 935650354@qq.com #@site: https://www.cnblogs.com/jackzz import re from time import sleep from selenium import webdriver import random import requests from PIL import Image from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from io import BytesIO from selenium.webdriver.common.action_chains import ActionChains USERNAME = '123456' PASSWORD = '123456' class BiliLogin(object): def __init__(self): ''' 初始化 ''' self.login_url = 'https://passport.bilibili.com/login' self.driver = webdriver.Firefox() self.wait = WebDriverWait(self.driver,10) self.driver.maximize_window() def input_info(self): ''' 輸入帳號密碼 :return: ''' self.driver.get(self.login_url) username = self.wait.until(EC.presence_of_element_located((By.ID,'login-username'))) password = self.wait.until(EC.presence_of_element_located((By.ID,'login-passwd'))) username.send_keys(USERNAME) password.send_keys(PASSWORD) sleep(1) def click_login_button(self): ''' 點擊登錄按鈕 :return: ''' login_button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'btn-login'))) login_button.click() sleep(1) def get_captcha_image(self): ''' 獲取驗證碼圖片 :return: ''' geetest_canvas_bg = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,'geetest_canvas_bg'))) geetest_canvas_bg.screenshot('bg.png') image_bg = Image.open('bg.png') #經過js代碼修改標籤樣式 顯示圖片2 js = 'var change = document.getElementsByClassName("geetest_canvas_fullbg");change[0].style = "display:block;"' self.driver.execute_script(js) sleep(3) geetest_canvas_fullbg = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,'geetest_canvas_fullbg'))) geetest_canvas_fullbg.screenshot('fullbg.png') image_fullbg = Image.open('fullbg.png') return image_bg,image_fullbg def get_diff_location(self,image1, image2): ''' 經過像素對比 找到缺口位置 :param image1: :param image2: :return: ''' for x in range(65, image1.size[0]): for y in range(image1.size[1]): if self.is_similar(image1, image2, x, y) == False: # 判斷成立 表示xy這個點 兩張圖不同 return x def is_similar(self,image1, image2, x, y): pixel1 = image1.getpixel((x, y)) pixel2 = image2.getpixel((x, y)) for i in range(0, 3): if abs(pixel1[i]) - pixel2[i] >= 60: return False return True def get_track(self,x): ''' 滑塊移動軌跡 初速度 v =0 單位時間 t = 0.2 位移軌跡 tracks = [] 當前位移 ccurrent = 0 :param x: :return: ''' v = 0 t = 0.2 tracks = [] current = 0 mid = x*4/5#到達mid值開始減速 while current < x: if current < mid: a = random.randint(1,3) else: a = -random.randint(2,4) # a = 2 v0 = v # 單位時間內位移公式 s = v0 * t + 0.5 * a * (t ** 2) # 當前位移 current = current + s tracks.append(round(s)) v = v0 + a * t return tracks def move_to_xoffset(self,tracks): element = self.driver.find_element_by_class_name('geetest_slider_button') ActionChains(self.driver).click_and_hold(element).perform() for x in tracks: ActionChains(self.driver).move_by_offset(xoffset=x, yoffset=0).perform() ActionChains(self.driver).release(element).perform() sleep(3) def main(self): self.input_info() self.click_login_button() image_bg,image_fullbg = self.get_captcha_image() xoffset = self.get_diff_location(image_bg,image_fullbg) print(xoffset) tracks = self.get_track(xoffset-7) print(tracks) self.move_to_xoffset(tracks) sleep(2) try: self.driver.find_elements_by_class_name('xxxxxxx') except: self.driver.refresh() self.main() if __name__ == '__main__': bili = BiliLogin() try: count = 5 while count > 0: bili.main() count -= 1 except Exception as e: print('識別錯誤,繼續') finally: print('恭喜經過滑塊驗證') sleep(2) bili.driver.quit()