准备工具

pip3 install PIL

pip3 install opencv-python

pip3 install numpy

谷歌驱动

建议指定清华源下载速度会更快点

使用方法 :pip3 install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/opencv-python/

谷歌驱动

谷歌驱动下载链接 :http://npm.taobao.org/mirrors/chromedriver/

前言

本篇文章采用的是cv2的Canny边缘检测算法进行图像识别匹配。

Canny边缘检测算法参考链接:https://www.jb51.net/article/185336.htm

具体使用的是Canny的matchTemplate方法进行模糊匹配,匹配方法用CV_TM_CCOEFF_NORMED归一化相关系数匹配。得出的max_loc就是匹配出来的位置信息。从而达到位置的距离。

难点

由于图像采用放大的效果匹配出的距离偏大,难以把真实距离,并存在误差。

由于哔哩哔哩滑块验证进一步采取做了措施,如果滑动时间过短,会导致验证登入失败。所以我这里采用变速的方法,在相同时间内滑动不同的距离。

误差的存在是必不可少的,有时会导致验证失败,这都是正常现象。

流程

1.实例化谷歌浏览器 ,并打开哔哩哔哩登入页面。

2.点击登陆,弹出滑动验证框。

3.全屏截图、后按照尺寸裁剪各两张。

5.模糊匹配两张图片,从而获取匹配结果以及位置信息 。

6.将位置信息与页面上的位移距离转化,并尽可能少的减少误差 。

7.变速的拖动滑块到指定位置,从而达到模拟登入。

效果图

代码实例

库安装好后,然后填写配置区域后即可运行。

from PIL import Image

from time import sleep

from selenium import webdriver

from selenium.webdriver import ActionChains

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

import cv2

import numpy as np

import math

############ 配置区域 #########

zh="" #账号

pwd="" #密码

# chromedriver的路径

chromedriver_path = "C:Program Files (x86)GoogleChromeApplicationchromedriver.exe"

####### end #########

options = webdriver.ChromeOptions()

options.add_argument("--no-sandbox")

options.add_argument("--window-size=1020,720")

# options.add_argument("--start-maximized") # 浏览器窗口最大化

options.add_argument("--disable-gpu")

options.add_argument("--hide-scrollbars")

options.add_argument("test-type")

options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors",

"enable-automation"]) # 设置为开发者模式

driver = webdriver.Chrome(options=options, executable_path=chromedriver_path)

driver.get("https://passport.bilibili.com/login")

# 登入

def login():

driver.find_element_by_id("login-username").send_keys(zh)

driver.find_element_by_id("login-passwd").send_keys(pwd)

driver.find_element_by_css_selector("#geetest-wrap > div > div.btn-box > a.btn.btn-login").click()

print("点击登入")

# 整个图,跟滑块整个图

def screen(screenXpath):

img = WebDriverWait(driver, 20).until(

EC.visibility_of_element_located((By.XPATH, screenXpath))

)

driver.save_screenshot("allscreen.png") # 对整个浏览器页面进行截图

left = img.location["x"]+160 #往右

top = img.location["y"]+60 # 往下

right = img.location["x"] + img.size["width"]+230 # 往左

bottom = img.location["y"] + img.size["height"]+110 # 往上

im = Image.open("allscreen.png")

im = im.crop((left, top, right, bottom)) # 对浏览器截图进行裁剪

im.save("1.png")

print("截图完成1")

screen_two(screenXpath)

screen_th(screenXpath)

matchImg("3.png","2.png")

# 滑块部分图

def screen_two(screenXpath):

img = WebDriverWait(driver, 20).until(

EC.visibility_of_element_located((By.XPATH, screenXpath))

)

left = img.location["x"] + 160

top = img.location["y"] + 80

right = img.location["x"] + img.size["width"]-30

bottom = img.location["y"] + img.size["height"] + 90

im = Image.open("allscreen.png")

im = im.crop((left, top, right, bottom)) # 对浏览器截图进行裁剪

im.save("2.png")

print("截图完成2")

# 滑块剩余部分图

def screen_th(screenXpath):

img = WebDriverWait(driver, 20).until(

EC.visibility_of_element_located((By.XPATH, screenXpath))

)

left = img.location["x"] + 220

top = img.location["y"] + 60

right = img.location["x"] + img.size["width"]+230

bottom = img.location["y"] + img.size["height"] +110

im = Image.open("allscreen.png")

im = im.crop((left, top, right, bottom)) # 对浏览器截图进行裁剪

im.save("3.png")

print("截图完成3")

#图形匹配

def matchImg(imgPath1,imgPath2):

imgs = []

#展示

sou_img1= cv2.imread(imgPath1)

sou_img2 = cv2.imread(imgPath2)

# 最小阈值100,最大阈值500

img1 = cv2.imread(imgPath1, 0)

blur1 = cv2.GaussianBlur(img1, (3, 3), 0)

canny1 = cv2.Canny(blur1, 100, 500)

cv2.imwrite("temp1.png", canny1)

img2 = cv2.imread(imgPath2, 0)

blur2 = cv2.GaussianBlur(img2, (3, 3), 0)

canny2 = cv2.Canny(blur2, 100, 500)

cv2.imwrite("temp2.png", canny2)

target = cv2.imread("temp1.png")

template = cv2.imread("temp2.png")

# 调整大小

target_temp = cv2.resize(sou_img1, (350, 200))

target_temp = cv2.copyMakeBorder(target_temp, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=[255, 255, 255])

template_temp = cv2.resize(sou_img2, (200, 200))

template_temp = cv2.copyMakeBorder(template_temp, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=[255, 255, 255])

imgs.append(target_temp)

imgs.append(template_temp)

theight, twidth = template.shape[:2]

# 匹配跟拼图

result = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED)

cv2.normalize( result, result, 0, 1, cv2.NORM_MINMAX, -1 )

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

# 画圈

cv2.rectangle(target,max_loc,(max_loc[0]+twidth,max_loc[1]+theight),(0,0,255),2)

target_temp_n = cv2.resize(target, (350, 200))

target_temp_n = cv2.copyMakeBorder(target_temp_n, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=[255, 255, 255])

imgs.append(target_temp_n)

imstack = np.hstack(imgs)

cv2.imshow("windows"+str(max_loc), imstack)

cv2.waitKey(0)

cv2.destroyAllWindows()

# 计算距离

print(max_loc)

dis=str(max_loc).split()[0].split("(")[1].split(",")[0]

x_dis=int(dis)+135

t(x_dis)

#拖动滑块

def t(distances):

draggable = driver.find_element_by_css_selector("div.geetest_slider.geetest_ready > div.geetest_slider_button")

ActionChains(driver).click_and_hold(draggable).perform() #抓住

print(driver.title)

num=getNum(distances)

sleep(3)

for distance in range(1,int(num)):

print("移动的步数: ",distance)

ActionChains(driver).move_by_offset(xoffset=distance, yoffset=0).perform()

sleep(0.25)

ActionChains(driver).release().perform() #松开

# 计算步数

def getNum(distances):

p = 1+4*distances

x1 = (-1 + math.sqrt(p)) / 2

x2 = (-1 - math.sqrt(p)) / 2

print(x1,x2)

if x1>=0 and x2<0:

return x1+2

elif(x1<0 and x2>=0):

return x2+2

else:

return x1+2

def main():

login()

sleep(5)

screenXpath = "/html/body/div[2]/div[2]/div[6]/div/div[1]/div[1]/div/a/div[1]/div/canvas[2]"

screen(screenXpath)

sleep(5)

if __name__ == "__main__":

main()

有能力的可以研究一下思路,然后写出更好的解决办法。

到此这篇关于python模拟哔哩哔哩滑块登入验证的实现的文章就介绍到这了,更多相关python 滑块登入验证内容请搜索云海天教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持云海天教程!

python弹出滑块怎么验证_python模拟哔哩哔哩滑块登入验证的实现相关推荐

  1. python弹出窗口后卡死_python的tkinter模块GUI编程为啥用了while循环之后就会使得程序出现卡死未响应崩溃?...

    这位同学,首先无代码无真相.只能在这里猜测一下,你在GUI界面中点击了某个按钮,调用的函数然后触发了某种while循环,这个时候前台GUI将"未响应"卡死.不过一旦调用函数的whi ...

  2. python模拟哔哩哔哩滑块登入验证

    python模拟哔哩哔哩滑块登入验证 准备工具 pip3 install PIL pip3 install opencv-python pip3 install numpy 谷歌驱动 建议指定清华源下 ...

  3. 解决win10 cmd下运行python弹出windows应用商店问题

    解决win10 cmd下运行python弹出windows应用商店问题 问题描述: ​ win10系统下,环境变量已配置,然而在cmd下或powershell下运行python,均弹出应用商店,不能正 ...

  4. win10系统cmd模式下输入python弹出Windows应用商店 解决方法

    win10系统cmd模式下输入python弹出Windows应用商店 解决方法 解决方法1: 打开环境变量设置[此电脑->属性->高级系统设置->环境变量] 在变量Path中发现有% ...

  5. 【Python】关于Python弹出Invalid SDK Permission Denied问题的解决

    [Python]关于Python弹出Invalid SDK Permission Denied问题的解决 背景:本人今天在电脑上安装了Pycharm IDE,但是却总是弹出Invalid Python ...

  6. cmd输入python弹出应用商店

    如果cmd输入python弹出应用商店,环境变量没问题,只需要把这两个关掉即可. https://blog.csdn.net/qq_43706426/article/details/104347702 ...

  7. python弹出滑块怎么验证_selenium 处理滑块验证的重点

    前言 在爬取数据中,不可避免的会遇到各种验证,上一篇文章说了二维码验证的办法 示例 本篇文章讲的是遇到滑块验证的解决方案,本例是在一个b2b的网站内遇到的滑块验证 重点 真实人的拖动操作,是y轴和x都 ...

  8. python 弹出对话框_python+selenium 抓取弹出对话框信息

    抓取弹出对话框信息,困挠了我很久,我百度了很久,一直没有找到我想要的内容.最近学习到了. 有两种方法: 1.driver.switch_to.alert.text 2.result = EC.aler ...

  9. python弹出输入框_Python实现使用tkinter弹出输入框输入数字, 具有确定输入和清除功能...

    Python3.6中用tkinter, 弹出可以输入数字的输入框. # Copyright (c) 2017-7-21 ZhengPeng All rights reserved. def pop_u ...

  10. python弹出警告框_selenium+webdriver+python 中警告框的处理方法

    在自动化测试过程中,经常会遇到弹出警告框的情况,如图所示: 在 WebDriver 中处理 JavaScript 所生成的 alert.confirm 以及 prompt 是很简单的.具体做法是使用 ...

最新文章

  1. 500页开放书搞定概率图建模,图灵奖得主Judea Pearl推荐(附链接)
  2. linux命令--df命令du命令
  3. 如何快速在Linux系统的硬盘上创建大文件
  4. mysql从库同步delete不动了_MySQL主从同步报错故障处理集锦
  5. 把HttpClient换成IHttpClientFactory之后,放心多了
  6. ObjectArx创建指定块
  7. Innodb ibdata数据文件误删,如何恢复
  8. 想提高运维效率,那就把MySQL数据库部署到Kubernetes 集群中
  9. vim 寄存器 操作_说实话,Intellij IDEA 自带的 Vim 插件真心不错。。。
  10. Linux文件类型与文件权限详解(三)
  11. class path resource [spring/] cannot be resolved to URL because it does not exist
  12. 2021年中国研究生数学建模竞赛D题——抗乳腺癌候选药物的优化建模
  13. USB Server应用于RPA机器人案例分析
  14. JS日历控件 (兼容IE firefox) 可选择时间
  15. Verilog HDL实战操作①——基本门电路
  16. NH2-UiO-66,CAS:1260119-00-3
  17. 在Vmware14中安装Linux系统教程(图文教程)
  18. BIOS设置u盘启动找不到u盘选项怎么办?
  19. 优秀的LOGO设计都有哪些共同点,是需要我们借鉴的?
  20. 【欺骗眼睛】可能你不会相信,图中的A色块和B色块是同一个颜色

热门文章

  1. xlsx表格怎么做汇总统计_用excel表格统计数据-如何将多个EXCEL表格的数据进行汇总?...
  2. 因果信号的傅里叶变换_信号傅里叶变换系列文章(1):傅里叶级数、傅里叶系数以及傅里叶变换...
  3. udp wpf 权限_基于WPF开发局域网聊天工具,在用udp做上线功能时遇到的有关问题...
  4. 如何去除计算机病毒,怎么清除计算机病毒
  5. VB程序VB代码:摄像头视频图像的监控,截图,录像(改进)
  6. 任我行CRM8.4破解版,任我行破解版免费下载,v8.4完整破解稳定版【捡肥皂】
  7. MSDN帮助文档中文
  8. 有一种爱情,叫沉、重!
  9. 我心中的你是春天的样子
  10. 网易游戏(雷火)一、二、三交叉面