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()
相关推荐
读创商闻36 分钟前
极狐GitLab CEO 柳钢——极狐 GitLab 打造中国企业专属 AI 编程平台,引领编程新潮流
人工智能·gitlab
kailp36 分钟前
语言模型玩转3D生成:LLaMA-Mesh开源项目
人工智能·3d·ai·语言模型·llama·gpu算力
marteker37 分钟前
弗兰肯斯坦式的人工智能与GTM策略的崩溃
人工智能·搜索引擎
无心水39 分钟前
大语言模型零样本情感分析实战:无需机器学习训练,96%准确率实现指南
人工智能·机器学习·语言模型
来自于狂人39 分钟前
AI大模型训练的云原生实践:如何用Kubernetes指挥千卡集群?
人工智能·云原生·kubernetes
千宇宙航6 小时前
闲庭信步使用图像验证平台加速FPGA的开发:第十四课——图像二值化的FPGA实现
图像处理·计算机视觉·fpga开发
橡晟6 小时前
深度学习入门:让神经网络变得“深不可测“⚡(二)
人工智能·python·深度学习·机器学习·计算机视觉
墨尘游子6 小时前
神经网络的层与块
人工智能·python·深度学习·机器学习
Leah01056 小时前
什么是神经网络,常用的神经网络,如何训练一个神经网络
人工智能·深度学习·神经网络·ai
PyAIExplorer7 小时前
图像亮度调整的简单实现
人工智能·计算机视觉