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()
相关推荐
FL162386312919 分钟前
AI健身体能测试之基于paddlehub实现引体向上计数个数统计
人工智能
黑客-雨23 分钟前
构建你的AI职业生涯:从基础知识到专业实践的路线图
人工智能·产品经理·ai大模型·ai产品经理·大模型学习·大模型入门·大模型教程
子午25 分钟前
动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
人工智能·python·cnn
大耳朵爱学习42 分钟前
掌握Transformer之注意力为什么有效
人工智能·深度学习·自然语言处理·大模型·llm·transformer·大语言模型
TAICHIFEI44 分钟前
目标检测-数据集
人工智能·目标检测·目标跟踪
qq_15321452641 小时前
【2023工业异常检测文献】SimpleNet
图像处理·人工智能·深度学习·神经网络·机器学习·计算机视觉·视觉检测
洛阳泰山1 小时前
如何使用Chainlit让所有网站快速嵌入一个AI聊天助手Copilot
人工智能·ai·llm·copilot·网站·chainlit·copliot
儿创社ErChaungClub1 小时前
解锁编程新境界:GitHub Copilot 让效率翻倍
人工智能·算法
乙真仙人1 小时前
AIGC时代!AI的“iPhone时刻”与投资机遇
人工智能·aigc·iphone
十启树1 小时前
用Qt 对接‌百度AI平台
人工智能·qt·百度