OpenCV图片验证码识别与滑块验证码识别

目录

图片验证码识别:

一、百度OCR文字识别云服务

二、维普网获取图片验证码

三、维普网opencv+ocr识别验证码

四、维普网selenium登录并获取数据

滑块验证码:

五、猎聘网获取滑块验证码

六、猎聘网opencv计算滑动距离

七、猎聘网selenium模拟滑动轨迹


图片验证码识别:

一、百度OCR文字识别云服务

百度云-产品-搜索:文字识别-通用场景文字识别-立即使用-开通(实名认证)-免费尝鲜-创建新应用-调用服务-通用场景文字识别(高精度版)-复制相关代码

需要终端安装base64:pip install pybase64

python 复制代码
import requests
import base64

host = 'https://aip.baibubce.com/pauth/2.0/token?grant_type=client_credentials&client_id=[官网获取的AK]&client_secret=[官网获取的SK]'
response = requests.get(host)
if response:
    access_token = reponse.json()["access_token"]
    print(access_token)

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
# 二进制方式打开图片文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())

params = {"image":img}
# access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" +access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print(response.json()["words_result"][0]["words"])

二、维普网获取图片验证码

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time
import cv2
import requests
 
service = Service(executable_path="../_resources/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("http://my.cqvip.com/login")
time.sleep(1)
 
img = driver.find_element(By.XPATH, '//*[@id="verifycode"]')
img.screenshot("./img/captcha.png")
 
time.sleep(1)
driver.quit()

三、维普网opencv+ocr识别验证码

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import json
import time
import cv2
import requests
import base64

service = Service(executable_path="../_resources/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("http://my.cqvip.com/login")
time.sleep(1)
 
img = driver.find_element(By.XPATH, '//*[@id="verifycode"]')
img.screenshot("./img/captcha.png")
 

img = cv2.imread('./img/captcha.png', flags=cv2.IMREAD_GRAYSCALE)
thresh, img = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
cv2.imwrite("./img/captcha2.png", img)

host = 'https://aip.baibubce.com/oauth/2.0/token?client_id=xxxxxx&client_secret=xxxxxx&grant_type=client_credentials'
payload = ""
headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
}
response = requests.request("Post", url, headers=headers, data=payload)
access_token = reponse.json()["access_token"]


request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
f = open('./img/captcha2.png', 'rb')
img = base64.b64encode(f.read())

params = {"image":img}
request_url = request_url + "?access_token=" +access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print(response.json()["words_result"][0]["words"])

time.sleep(1)
driver.quit()

四、维普网selenium登录并获取数据

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import json
import time
import cv2
import requests
import base64

service = Service(executable_path="../_resources/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("http://my.cqvip.com/login")
time.sleep(1)

username = driver.find_element(By.XPATH, '//*[@id="txtLoginUserName"]')
ActionChains(driver).pause(0.5).click(username).send_keys("xxxxx").perform()

password = driver.find_element(By.XPATH, '//*[@id="txtLoginPass"]')
ActionChains(driver).pause(0.5).click(password).send_keys("xxxxx").perform()
 
while driver.current_url == "http://my.cqvip.com/login":
    img = driver.find_element(By.XPATH, '//*[@id="verifycode"]')
    img.screenshot("./img/captcha.png")
 

    img = cv2.imread('./img/captcha.png', flags=cv2.IMREAD_GRAYSCALE)
    thresh, img = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
    cv2.imwrite("./img/captcha2.png", img)

    host = 'https://aip.baibubce.com/oauth/2.0/token?    client_id=xxxxxx&client_secret=xxxxxx&grant_type=client_credentials'
    payload = ""
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    response = requests.request("Post", url, headers=headers, data=payload)
    access_token = reponse.json()["access_token"]


    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
    f = open('./img/captcha2.png', 'rb')
    img = base64.b64encode(f.read())

    params = {"image":img}
    request_url = request_url + "?access_token=" +access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        result = response.json()["words_result"][0]["words"]
        print(result)

    cap = driver.find_element(By.XPATH, '//*[@id="validatecode"]')
    cap.clear()
    ActionChains(driver).pause(0.5).click(cap).send_keys(result).perform()

    submit = driver.find_element(By.XPATH, '//*[@id="btnAccountLogin"]')
    submit.click()
    time.sleep(4)

print(driver.page_source)
 
time.sleep(1)
driver.quit()

滑块验证码:

五、猎聘网获取滑块验证码

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import cv2
import time
import requests
import numpy

service = Service(executable_path="../_resources/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("http://www.liepin.com/")
time.sleep(1)

select = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[2]/div/div[2]')
select.click()

username = driver.find_element(By.XPATH, '//*[@id="login"]')
ActionChains(driver).pause(0.5).click(username).send_keys("xxxxx").perform()

password = driver.find_element(By.XPATH, '//*[@id="pwd"]')
ActionChains(driver).pause(0.5).click(password).send_keys("xxxxx").perform()

clickable = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[4]/div/label/span[1]/input')
ActionChains(driver).pause(0.5).click(clickable).perform()
 
submit = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[3]/div/form/button')
ActionChains(driver).pause(0.5).click(submit).perform()

time.sleep(2)
driver.switch_to.frame("tcaptcha_iframe")

back = driver.find_element(By.XPATH, '//*[@id="slideBg"]').get_attribute("src")
res_back = requests.get(back)
with open("./img2/back.png", "wb") as f:
    f.write(res_back.content)

front = driver.find_element(By.XPATH, '//*[@id="slideBlock"]').get_attribute("src")
res_front = requests.get(front)
with open("./img2/front.png", "wb") as f:
    f.write(res_front.content)
 
time.sleep(3)
driver.quit()

六、猎聘网opencv计算滑动距离

python 复制代码
import cv2
import numpy

back = cv2.imread('./img2/back.png', flags=cv2.IMREAD_GRAYSCALE)
front = cv2.imread('./img2/front.png', flags=cv2.IMREAD_GRAYSCALE)

front = front[24:front.shape[0]-24, 24:front.shape[0]-24]

thresh, back = cv2.threshold(back, 110, 255, cv2.THRESH_BINARY)
thresh, front = cv2.threshold(front, 40, 255, cv2.THRESH_BINARY_INV)

cv2.imwrite('./img2/back2.png', back)
cv2.imwrite('./img2/front2.png', front)

match = cv2.matchTemplate(back, front, cv2.TM_CCORR_NORMED)
distance = cv2.minMaxLoc(match)[3][0]
print(distance)

# cv2.imshow("back", back)
# cv2.imshow("front", front)

# cv2.waitKey(0)
# cv2.destroyAllWindows()

七、猎聘网selenium模拟滑动轨迹

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import cv2
import time
import requests
import numpy

service = Service(executable_path="../_resources/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("http://www.liepin.com/")
time.sleep(1)

select = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[2]/div/div[2]')
select.click()

username = driver.find_element(By.XPATH, '//*[@id="login"]')
ActionChains(driver).pause(0.5).click(username).send_keys("xxxxx").perform()

password = driver.find_element(By.XPATH, '//*[@id="pwd"]')
ActionChains(driver).pause(0.5).click(password).send_keys("xxxxx").perform()

clickable = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[4]/div/label/span[1]/input')
ActionChains(driver).pause(0.5).click(clickable).perform()
 
submit = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[3]/div/form/button')
ActionChains(driver).pause(0.5).click(submit).perform()

time.sleep(2)
driver.switch_to.frame("tcaptcha_iframe")

while driver.current_url == "https://www.liepin.com/":
    refresh = driver.find_element(By.XPATH, '//*[@id="reload"]/div')
    refresh.click()
    time.sleep(1)

    back = driver.find_element(By.XPATH, '//*[@id="slideBg"]').get_attribute("src")
    res_back = requests.get(back)
    with open("./img2/back.png", "wb") as f:
        f.write(res_back.content)

    front = driver.find_element(By.XPATH, '//*[@id="slideBlock"]').get_attribute("src")
    res_front = requests.get(front)
    with open("./img2/front.png", "wb") as f:
        f.write(res_front.content)
 

    back = cv2.imread('./img2/back.png', flags=cv2.IMREAD_GRAYSCALE)
    front = cv2.imread('./img2/front.png', flags=cv2.IMREAD_GRAYSCALE)

    front = front[24:front.shape[0]-24, 24:front.shape[0]-24]

    thresh, back = cv2.threshold(back, 110, 255, cv2.THRESH_BINARY)
    thresh, front = cv2.threshold(front, 40, 255, cv2.THRESH_BINARY_INV)

    cv2.imwrite('./img2/back2.png', back)
    cv2.imwrite('./img2/front2.png', front)

    match = cv2.matchTemplate(back, front, cv2.TM_CCORR_NORMED)
    distance = cv2.minMaxLoc(match)[3][0]
    # 341:滑动验证码图片的长, 680:下载后图片的长, 37:滑块现在距离滑动验证码图片最左端的距离
    distance = distance * 341 // 680 - 37
    print(distance)

    slider = driver.find_element(By.XPATH, '//*[@id="tcaptcha_drag_thumb"]')
    ActionChains(driver).pause(0.2).click_and_hold(slider).pause(0.2).move_by_offset(distance / 4, 5).perform()   
    ActionChains(driver).pause(0.1).move_by_offset(distance / 2, -2).perform()
    ActionChains(driver).pause(0.1).move_by_offset(distance / 4, 3).release().perform()
    time.sleep(3)

driver.get("https://www.liepin.com/")
time.sleep(1)
print(driver.page_source)

time.sleep(3)
driver.quit()
相关推荐
昨夜见军贴061621 分钟前
IACheck AI审核在生产型企业质量控制记录中的实践探索——全面赋能有关物质研究合规升级
大数据·人工智能
智星云算力39 分钟前
智星云镜像共享全流程指南,附避坑手册(新手必看)
人工智能
盖雅工场41 分钟前
驱动千店销售转化提升10%:3C零售门店的人效优化实战方案
大数据·人工智能·零售·数字化管理·智能排班·零售排班
Loo国昌1 小时前
深入理解 FastAPI:Python高性能API框架的完整指南
开发语言·人工智能·后端·python·langchain·fastapi
发哥来了1 小时前
【AI视频创作】【评测】【核心能力与成本效益】
大数据·人工智能
醉舞经阁半卷书11 小时前
Python机器学习常用库快速精通
人工智能·python·深度学习·机器学习·数据挖掘·数据分析·scikit-learn
产品何同学2 小时前
在线问诊医疗APP如何设计?2套原型拆解与AI生成原型图实战
人工智能·产品经理·健康医疗·在线问诊·app原型·ai生成原型图·医疗app
星爷AG I2 小时前
9-14 知觉整合(AGI基础理论)
人工智能·agi
开源技术2 小时前
Violit: Streamlit杀手,无需全局刷新,构建AI面板
人工智能·python
递归尽头是星辰3 小时前
大模型与向量检索的融合:从核心原理到 Spring AI 落地
人工智能·大模型·向量检索·rag·spring ai·向量库