本文小编为大家详细介绍“怎么使用selenium+opencv实现滑块验证码的登陆”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么使用selenium+opencv实现滑块验证码的登陆”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
环境
python 3.9, selenium和Opencv相关依赖,用于抓取图片的requests包,具体安装这里不多讲了,其中selenium用的火狐版本。
selenium登录网站
整体流程就是这个样子:访问网站->点击登录->输入账号密码->搞定滑块验证->登录网站,其中最大的难点是滑块验证码,但在此之前我们当然要先让selenium自动打开网站把账号密码输好,我们通过find_element()方法定位输入框之后执行操作,元素的各个属性F12就可以找到:
代码如下:
options = webdriver.FirefoxOptions()driver = webdriver.Firefox(options=options)driver.get('网址')driver.find_element("link text", "登录").click()name = driver.find_element("id", "name-input")name.send_keys("账号######") # 输入账号pw = driver.find_element("id", "password-input")pw.send_keys("密码#########") # 输入密码driver.find_element("id", "submit").click() # 提交
requests抓取验证码图片
为了做后续处理我们需要把滑块验证码相关图片抓到本地,网上关于滑块验证码这块很多都是用原图和有缺口的图对比来确定缺口位置的,但是我并没有找到原图,这里用到的是有缺口的背景图和滑块图,如下:
滑块图:
有缺口的背景图:
这里爬图是selenium定位之后用requests包爬的,注意验证码和登陆界面不在一个iframe里,selenium记得切到对应iframe才能定位到图片,代码如下:
driver.switch_to.frame('tcaptcha_iframe') # 切换iframeimg = driver.find_element("id", "slideBg").get_attribute('src')headers = { 'Accept': "application/json, text/plain, **", 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" } driver.get('网址') driver.find_element("link text", "登录").click() name = driver.find_element("id", "name-input") name.send_keys(username) pw = driver.find_element("id", "password-input") pw.send_keys(password) driver.find_element("id", "submit").click() time.sleep(2) driver.switch_to.frame('tcaptcha_iframe') img = driver.find_element("id", "slideBg").get_attribute('src') r = requests.get(img, headers=headers) with open('img.png', 'wb') as f: f.write(r.content) block = driver.find_element("id", "slideBlock").get_attribute('src') r = requests.get(block, headers=headers) with open('block.png', 'wb') as f: f.write(r.content) if __name__ == '__main__': options = webdriver.FirefoxOptions() driver = webdriver.Firefox(options=options) user = "##########" pw = "############" login_in(user, pw) image = "img.png" tpl = "block.png" length = block_loc.match(image, tpl) print(length) drag_block(length)
Opencv部分:
import cv2import numpy as np def avg_mean(img): mean_val, _, _, _ = cv2.mean(img) print("平均灰度:", mean_val) return mean_val def match(image, temp): img = cv2.imread(image) tpl = cv2.imread(temp) tpl_gray = cv2.cvtColor(tpl, cv2.COLOR_BGR2GRAY) # cv2.imshow("111", tpl_gray) width, height = tpl_gray.shape for h in range(height): for w in range(width): if tpl_gray[w, h] == 0: tpl_gray[w, h] = 96 binary = cv2.inRange(tpl_gray, 96, 96) kernel = np.ones((8, 8), np.uint8) template = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel) # cv2.imshow('tpl', template) print(img.shape) gauss = cv2.GaussianBlur(img, [5, 5], 0) img_gray = cv2.cvtColor(gauss, cv2.COLOR_BGR2GRAY) # cv2.imshow("111", img_gray) if avg_mean(img) > 140: ret, target = cv2.threshold(img_gray, 105, 255, cv2.THRESH_BINARY) # 二值化 elif avg_mean(img) > 102: ret, target = cv2.threshold(img_gray, 95, 255, cv2.THRESH_BINARY) # 二值化 else: ret, target = cv2.threshold(img_gray, 80, 255, cv2.THRESH_BINARY) # cv2.imshow('target', target) result = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) left_up = max_loc print(left_up) right_down = (left_up[0] + height, left_up[1] + width) cv2.rectangle(img, left_up, right_down, (0, 0, 255), 2) # cv2.imshow('res', img) length = (left_up[0] - 26*2)/2 return length
读到这里,这篇“怎么使用selenium+opencv实现滑块验证码的登陆”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网行业资讯频道。