Python 官方文档:入门教程 => 点击学习
目录环境selenium登录网站requests抓取验证码图片OpenCV识别缺口位置模拟拖动滑块脚本示例:很多网站登录登陆时都要用到滑块验证码,在某些场景例如使用爬虫爬取信息时常常
很多网站登录登陆时都要用到滑块验证码,在某些场景例如使用爬虫爬取信息时常常受到阻碍,想着用opencv的模板匹配试试能不能实现模拟登陆。本来觉得网上资料多应该还蛮容易,但实际上手还是搞了蛮久,在这里记录一下整个流程,网站无所谓主要是要有滑动验证码:
python 3.9, selenium和Opencv相关依赖,用于抓取图片的requests包,具体安装这里不多讲了,其中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() # 提交
为了做后续处理我们需要把滑块验证码相关图片抓到本地,网上关于滑块验证码这块很多都是用原图和有缺口的图对比来确定缺口位置的,但是我并没有找到原图,这里用到的是有缺口的背景图和滑块图,如下:
滑块图:
有缺口的背景图:
这里爬图是selenium定位之后用requests包爬的,注意验证码和登陆界面不在一个iframe里,selenium记得切到对应iframe才能定位到图片,代码如下:
driver.switch_to.frame('tcaptcha_iframe')
# 切换iframe
img = 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 cv2
import 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实现滑块验证码的登陆的文章就介绍到这了,更多相关selenium opencv 滑块验证码内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: selenium+opencv实现滑块验证码的登陆
本文链接: https://lsjlt.com/news/209291.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0