Selenium自动填写验证码(偏小白版本OCR)

OCR基础示例

我直接

上代码

python 复制代码
from PIL import Image
import pytesseract

# 0 Orientation and script detection (OSD) only.
# 1 Automatic page segmentation with OSD.
# 2 Automatic page segmentation, but no OSD, or OCR.
# 3 Fully automatic page segmentation, but no OSD. (Default)
# 4 Assume a single column of text of variable sizes.
# 5 Assume a single uniform block of vertically aligned text.
# 6 Assume a single uniform block of text.
# 7 Treat the image as a single text line.
# 8 Treat the image as a single word.
# 9 Treat the image as a single word in a circle.
# 10 Treat the image as a single character.
# 11 Sparse text. Find as much text as possible in no particular order.
# 12 Sparse text with OSD.
# 13 Raw line. Treat the image as a single text line, bypassing hacks that are Tesseract-specific.


def get_chaptcha(psm):
    pytesseract.pytesseract.tesseract_cmd = r'D:\Tesseract-OCR\tesseract.exe'
    # 打开图像并转换为灰度图像
    image = Image.open('captcha.png').convert('L')
    # image = Image.open('captcha.png')
    # 使用 OCR 识别验证码
    captcha_text = pytesseract.image_to_string(image, config=f'--psm {psm}')
    print(captcha_text)
    return captcha_text


if __name__ == '__main__':
    get_chaptcha(1)

主要难点就是安装Tesseract-OCR了,去GItHub上搜一搜,去搜索引擎上搜一搜就能下载到了

然后就是使用tesseract_cmd 指定你的tesseract.exe的位置

Tesseract就是一个别人开发好的用来识别验证码的工具,下载好之后是为了给Python调用的

而pytesseract这个库就是为了调用Tesseract-OCR的

注意:OCR一共有13个模式,一般用索引为1的模式会比较通用,特别是像我这种小白,索引1的模式准没错

Selenium基础实战示例

python 复制代码
time.sleep(1)
captcha_png_tag = chrome.find_xpath('//*[@id="captchaImg"]')
# captcha_url = captcha_png_tag.get_attribute('src')
captcha_png_tag.screenshot('captcha.png')
time.sleep(1)
captcha_text = simple_ocr.get_chaptcha(1)
time.sleep(1)
print(captcha_text)
if captcha_text == "":
    login()

captcha_input_tag = chrome.find_xpath('//*[@id="captcha"]')
chrome.input_message_tag(captcha_text, captcha_input_tag)
  
time.sleep(2)
# 检查是否出现了验证码错误弹窗
if check_captcha_error(chrome.driver):
    close_all_alert(chrome.driver)
    chrome.driver.refresh()
    time.sleep(2)  # 等待2秒以确保页面已经加载
    login()

有的网站的验证码是动态验证码,不能用下载原图的方式获取验证码的图片

只能通过Selenium自带的截图功能截图保存,这个截图功能还是挺好用的,可以支持仅截图某个页面元素

注意:有时候OCR无法识别验证码,会返回空结果,这个时候就得刷新验证码或者刷新网页再次尝试登录了

相关推荐
MarkHD2 小时前
RPA进阶实战:从零打造智能票据处理机器人——OCR识别、Excel自动填报与邮件通知全流程
机器人·ocr·rpa
叫我黎大侠5 小时前
.NET 实战:调用千问视觉模型实现 OCR(车票识别完整教程)
阿里云·ai·c#·ocr·asp.net·.net·.netcore
石榴树下的七彩鱼5 小时前
OCR 识别接口哪个好?2026 年主流 OCR API 对比评测(附免费在线体验)
图像处理·人工智能·后端·计算机视觉·ocr·api·文字识别
AI人工智能+5 小时前
表格识别技术通过深度学习与计算机视觉,实现复杂表格的自动化解析与结构化输出
深度学习·计算机视觉·ocr·表格识别
石榴树下的七彩鱼1 天前
身份证 OCR 识别 API 接入详解(Python / Java 示例)
java·开发语言·人工智能·后端·python·ocr·api
石榴树下的七彩鱼1 天前
Python OCR 文字识别 API 接入完整教程
开发语言·人工智能·后端·python·ocr·api·图片识别
小陈phd1 天前
多模态大模型学习笔记(三十五)——OCR全景认知:从字符识别到多模态理解的百年演进
笔记·学习·ocr
AI人工智能+1 天前
文档抽取系统:OCR实现图像文本转换,结合大语言模型的语义理解能力,构建了高效的合同信息抽取系统
语言模型·自然语言处理·ocr·文档抽取
weixin_408099672 天前
python请求文字识别ocr api
开发语言·人工智能·后端·python·ocr·api·ocr文字识别
weixin_408099672 天前
【组合实战】OCR + 图片去水印 API:自动清洗图片再识别文字(完整方案 + 代码示例)
图像处理·后端·ocr·api·文字识别·去水印·ocr识别优化