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无法识别验证码,会返回空结果,这个时候就得刷新验证码或者刷新网页再次尝试登录了

相关推荐
武子康2 小时前
AI研究-119 DeepSeek-OCR PyTorch FlashAttn 2.7.3 推理与部署 模型规模与资源详细分析
人工智能·深度学习·机器学习·ai·ocr·deepseek·deepseek-ocr
AI人工智能+6 小时前
告别手动录入:文档抽取技术如何让RPA处理非结构化数据?
nlp·ocr·rpa·文档抽取
翔云 OCR API1 天前
VIN码识别接口技术解析:智能OCR解决方案
ocr
OpenBayes2 天前
OCR 新范式!DeepSeek 以「视觉压缩」替代传统字符识别;Bald Classification数据集助力高精度人像分类
人工智能·深度学习·分类·数据挖掘·ocr·数据集·deepseek
AI人工智能+2 天前
文档抽取技术:通过OCR、关键信息抽取和数据标准化三大环节,实现了对银行流水的高效解析
nlp·ocr·文档抽取
亚林瓜子2 天前
在amazon linux 2023上面源码手动安装tesseract5.5.1
linux·运维·服务器·ocr·aws·ec2
翔云 OCR API3 天前
承兑汇票识别接口原理-OCR赋能票据自动化处理
运维·自动化·ocr
武子康3 天前
AI研究-121 DeepSeek-OCR 研究路线:无限上下文、跨模态抽取、未来创意点、项目创意点
人工智能·深度学习·机器学习·ai·ocr·deepseek·deepseek-ocr
X.Cristiano3 天前
DeepSeek-OCR和Glyph用视觉压缩思路对比
ocr·多模态·视觉压缩
亚林瓜子3 天前
AWS Elastic Beanstalk中安装tesseract5.3.4版本
spring boot·ocr·tesseract·aws·beanstalk·tess4j·eb