爬虫之图形验证码的识别

62 篇文章 5 订阅

一  点睛

1 举例

中国知网:http://my.cnki.net/elibregister/commonRegister.aspx 的注册页面使用的是这种验证码,页面如下:

二 准备工作

1 目标

以知网的验证码为例,利用OCR(Optical Character Recognition 光学字符识别)技术识别图形验证码。

2 安装tesseract

2.1 Windows下下载地址

https://digi.bib.uni-mannheim.de/tesseract/

2.2 下载tesseract-ocr-setup-3.05.01.exe

2.3 安装注意事项

勾选Additional language data(download)选项,这样可以识别多国语言。

3 安装tesserocr

pip install tesserocr pillow

参考下面两篇文章。

https://blog.csdn.net/xiongzaiabc/article/details/82908209

https://blog.csdn.net/t1243_3/article/details/81211029

安装好的Tesseract-OCR后,从D:\Program Files (x86)\Tesseract-OCR目录下,将tessdata文件夹拷贝到下面目录

E:\WebSpider\venv\Scripts

4 获取验证码

将验证码:http://my.cnki.net/elibregister/CheckCode.aspx图形 保存到本地,命名为code.jpg

三 实战

1 实战1

1.1 代码

import tesserocr
from PIL import Image

image = Image.open('code.jpg')
result = tesserocr.image_to_text(image)
print(result)

image = Image.open('code1.jpg')
result = tesserocr.image_to_text(image)
print(result)

image = Image.open('code2.jpg')
result = tesserocr.image_to_text(image)
print(result)

1.2 效果

E:\WebSpider\venv\Scripts\python.exe E:/WebSpider/8_1.py
DTKD
JR42
PFRT

1.3 说明

code.jpg是DTKT

code1.jpg是JR42

code2.jpg是PFRT

将结果和实际图片进行比较,正确率还是比较高的。

2 实战2

2.1 代码

import tesserocr

print(tesserocr.file_to_text('code.jpg'))
print(tesserocr.file_to_text('code1.jpg'))
print(tesserocr.file_to_text('code2.jpg'))

2.2 效果

E:\WebSpider\venv\Scripts\python.exe E:/WebSpider/8_1.py
DTKD
.ll?42
FFKT

2.3 说明

code.jpg是DTKT

code1.jpg是JR42

code2.jpg是PFRT

将结果和实际图片进行比较,正确率并不是很高。

3 实战3

3.1 代码

import tesserocr
from PIL import Image

image = Image.open('code2.jpg')

image = image.convert('L')
threshold = 127
table = []
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)

image = image.point(table, '1')
image.show()

result = tesserocr.image_to_text(image)
print(result)

3.2 效果

E:\WebSpider\venv\Scripts\python.exe E:/WebSpider/8_1.py
PFRT

 

 

 

 

 

  • 0
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值