[Python]B站登录的滑动验证

思路:
1.获取滑动的背景图(B站的滑动模块位置是不固定的,另外源码里又没找到原图,所以就写个循环不停的获取图片,然后PS进行拼接,得出正确的背景图
2.判断原背景图和加了滑动模块的背景图的像素差距来找出x轴的移动距离
3.使用位移公式来求出正常人的滑动模块轨迹
4.使用python+selenium完成模拟人手鼠标的拖拽
5.完成破解

from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import os
from PIL import Image
from selenium.webdriver import ActionChains
import random
 
 
def get_snap(driver):
    driver.save_screenshot('snap.png')
    snap_obj = Image.open('snap.png')
    return snap_obj
 
 
def get_image(driver):
    image = driver.find_element_by_class_name('geetest_canvas_slice')
    time.sleep(1)
    size = image.size
    location = image.location
    left = location['x']
    top = location['y']
    right = left + size['width']
    bottom = top + size['height']
    snap_obj = get_snap(driver)
    img_obj = snap_obj.crop((left, top, right, bottom))
    img_obj.save('image1.png')
    for i in range(1, 11):
        image2 = Image.open('bilibili_image/image{}.png'.format(str(i)))
        rgb = (image2.load()[58, 20])
        rgb2 = (img_obj.load()[58, 20])
        if rgb[0]==rgb2[0] and rgb[1]==rgb2[1] and rgb[2]==rgb2[2]:
            img_obj.save('image1.png')
            return img_obj, image2
 
 
def find_jl(image_obj1, image_obj2):
    start_x = 58
    # 从坐标系(60,25开始往下扫描)
    start_y = 20
    for x in range(start_x, image_obj1.size[0]):
        for y in range(start_y, image_obj1.size[1] - 16):
            rgb = image_obj1.load()[x, y]
            rgb2 = image_obj2.load()[x, y]
            if abs(rgb[0] - rgb2[0]) > 60 and abs(rgb[1] - rgb2[1]) > 60 and abs(rgb[2] - rgb2[2]) > 60:
                # print(rgb)
                return x - 6
 
 
def get_tracks(distance):
    distance += 10
    # v = V0+a*t
    # S = v*t+0.5*a*(t**2)
    v0 = 0
    s = 0
    t = 0.2
    mid = distance * 3 / 5
    forward_tracks = []
    back_tracks = [0, -1, -1, -1, -2, -2, -2, -1, -1, 0, 0, 1]
    while s < distance:
        if s < mid:
            a = 2
        else:
            a = -3
        v = v0
        track = v * t + 0.5 * a * (t ** 2)
        track = round(track)
        v0 = v + a * t
        s += track
        forward_tracks.append(track)
    return forward_tracks, back_tracks
 
 
def RUN(driver, track):
    slider_button = driver.find_element_by_class_name('geetest_slider_button')
    ActionChains(driver).click_and_hold(slider_button).perform()
    time.sleep(0.3)
    for i in track[0]:
        ActionChains(driver).move_by_offset(xoffset=i, yoffset=0).perform()
    time.sleep(0.2)
    for j in track[1]:
        ActionChains(driver).move_by_offset(xoffset=j, yoffset=0).perform()
    time.sleep(0.3)
    ActionChains(driver).release().perform()
 
 
def main(user_id, password):
    os.system("taskkill /f /im chromedriver.exe")
    url = 'https://passport.bilibili.com/login'
    driver = webdriver.Chrome()
    # 1.登录页面
    # driver = webdriver.Chrome()
    timeout = WebDriverWait(driver, 3)
    driver.get(url)
    # 2.输入公司名称,点击查询
    timeout.until(EC.element_to_be_clickable((By.ID, 'login-username')))
    user_button = driver.find_element_by_id('login-username')
    user_button.send_keys(user_id)
    time.sleep(random.uniform(0, 1))
    password_button = driver.find_element_by_id('login-passwd')
    password_button.send_keys(password)
    login_button = driver.find_element_by_class_name('btn-login')
    login_button.click()
    time.sleep(1)
    # 3.得到滑动图片
    # for i in range(100):
    #     if i >= 1:
    #         driver.find_element_by_class_name('geetest_panel_error_content').click()
    #         time.sleep(1)
    #     for j in range(5):
    #         get_image(driver, str(i)+str(j))
    #         time.sleep(0.5)
    #         driver.find_element_by_class_name('geetest_refresh_1').click()
    #         time.sleep(1)
    image_obj = get_image(driver)
    # 4.计算滑动距离
    distance = find_jl(image_obj[0], image_obj[1])
    # 5.通过动力加速度算法,算出人手滑动轨迹
    track = get_tracks(distance)
    # 6.执行
    RUN(driver, track)
    # 7.关闭浏览器
    time.sleep(5)
    driver.close()
 
 
if __name__ == '__main__':
    user_id = 'a123456'
    password = 'b123456'
    main(user_id, password)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大富大贵7

很高兴能够帮助到你 感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值