如何用Python脚本解决自动化图形验证码难题

1.环境准备

下载安装下面三个代码库:

  • Pytesseract

  • Tesseract-OCR

  • Pillow

1). 手动安装pytesseract库

通过cmd输入pip install pytesseract进行安装,但是安装后并不能直接使用,还需要下载Tesseract-OCR。

  • Python-tesseract是一个独立封装包;
  • Python-tesseract功能是识别图片文件中文字,并作为返回参数返回识别结果;
  • Python-tesseract默认支持tiff、bmp格式图片,只有在安装PIL之后,才能支持jpeg、gif、png等其他图片格式;

 2). 下载安装 Tesseract-OCR

  • tesseract是Python的一个OCR(光学字符识别)库,
  • 首先下载tesseract的exe安装文件   https://github.com/UB-Mannheim/tesseract/wiki
  • 下载完双击打开,连续next,直到出现安装路径的时候,可以自定义安装路径也可以使用默认的安装路径,但是无论是哪一种一定要记住路径“C:\Program Files\Tesseract-OCR”

  • 安装过程中勾选“Additional language data(download)” 用来支持多国语言

  •  配置环境变量 

  • 校验安装成功

  3). 安装Pillow包 

  • Python自带的图文简单处理模块,正常安装Python的时候会自动安装,故无需另外手动安装。
  • 若没自动安装则可手动安装:pip install Pillow

2.测试代码:

1). demo 脚本

from PIL import Image
import pytesseract
image = Image.open('d:/temp/1.png')
result = pytesseract.image_to_string(image)
print(result)

2). Demo图片 

3). 遇到的问题

执行过程中如果遇到如下错误:

C:\Users\28313\AppData\Local\Programs\Python\Python310\python.exe D:/pythonProject/testOCR.py
Traceback (most recent call last):
  File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 252, in run_tesseract
    proc = subprocess.Popen(cmd_args, **subprocess_args())
  File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\pythonProject\testOCR.py", line 4, in <module>
    result = pytesseract.image_to_string(image,lang='chi_sim')
  File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 413, in image_to_string
    return {
  File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 416, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
  File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 284, in run_and_get_output
    run_tesseract(**kwargs)
  File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 256, in run_tesseract
    raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

Process finished with exit code 1

4. 解决办法

修改C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py 文件内容

#tesseract_cmd = 'tesseract'
tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'

5. 执行结果

C:\Users\28313\AppData\Local\Programs\Python\Python310\python.exe D:/pythonProject/testOCR.py
   

Don't believe the things you tell yourself so late: Clmarreln)
They could be your w


Process finished with exit code 0

从结果可以看出:本方法只能识别一些简单纯净的英文,中文、数字、字母和标点符号的效果还是不错的,如果是经过处理的图片,比如验证码等图片的识别,需要借助jTessBoxEditor训练字库才能提高识别的准确率哦!

3. 自动化测试脚本中的应用

1). 步骤与思路

如果需要在脚本中实现,如果引用selenium的一些方法

  • 获取图片

  • 页面全屏截图

  • 截图转为Image对象

  • 获取指定图片的大小和位置

  • 裁剪图片

2). 代码例子  

import os
import pytesseract
import time
import selenium
from PIL import Image, ImageEnhance
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

path = 'd:/tools/chromedriver.exe'
url = "https://www.baidu.com/"
driver = webdriver.Chrome(executable_path=path)

driver.get(url)
driver.save_screenshot('input.png')
inputText = driver.find_element(By.NAME,'wd')
inputText.send_keys("selenium")
left = inputText.location['x']+61
top = inputText.location['y']+1
right = inputText.location['x'] + inputText.size['width']+47
bottom = inputText.location['y'] + inputText.size['height']-1
image = Image.open('input.png').crop((left,top,right,bottom))
image = image.convert('L') 			#转换模式:L | RGB
image = ImageEnhance.Contrast(image)            #增强对比度
image = image.enhance(2.0) 			#增加饱和度
image.save('input.png')                        #截图完成
code = pytesseract.image_to_string(image,lang='chi_sim')
print(code)

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈开发与测试

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值