当前位置:网站首页>都说验证码是爬虫中的一道坎,看我只用五行代码就突破它。
都说验证码是爬虫中的一道坎,看我只用五行代码就突破它。
2022-06-10 22:42:00 【程序员梨子】
前言
我相信很多刚学爬虫的新手,在学习过程中都会碰到验证码这块,其实这块也算是反爬这一块了。因为你运行代码抓取会一定程度对网站造成一系列的负担。所以此案例只用于学习交流。
在很久之前,分享过一次Python代码实现验证码识别的办法。
当时采用的是pillow+pytesseract,优点是免费,较为易用。但其识别精度一般,若想要更高要求的验证码识别,初学者就只能去选择使用百度API接口了。
但其实百度API接口和pytesseract其实都需要进行前期配置,对于初学者来说就不太友好了。
而且百度API必须要联网,对于某些机器不能联网的朋友而言,就得pass了
最近群里有位群友分享了一个新库,试用一下发现非常实用,特意今天分享给大家。
Github地址:关注公众号:Python顾木子即可获取。
该库名也是非常有趣 —— ddddocr(谐音带带弟弟OCR)
环境要求:
python >= 3.8 Windows/Linux/Macox..
可以通过以下命令安装
pip install ddddocr
参数说明:

在网上随机寻找了一个验证码图片,使用这个库来实战一下。

来源:百度搜索
import ddddocr
ocr = ddddocr.DdddOcr()
with open('1.png', 'rb') as f:
img_bytes = f.read()
res = ocr.classification(img_bytes)
print(res)

成功识别出来了验证码文字!
而且优点也非常明显:首先代码非常精简,对比前文提到的两种方法,不需要额外设置环境变量等等,5行代码即可轻松识别验证码图片。另一方面,我们使用魔法命令%%time也测试出来吗,这段代码识别速度非常快。
下面用更多的验证码图片继续测试:

我又找了6个验证码图片来测试,观察结果,发现这类简单的验证码基本可以进行快速识别。但也有部分结果有问题——字母大小写没有进行区分(比如第6张图片)。
总而言之,如果你需要进行验证码识别,且对精度要求不是过高。
肯定有好多人说你这都是直接用的别人家的库,没有库你咋个办呢?其实Python这门语言就是调用各大模块和框架的。不用重复造轮子又能达到想要的效果还是很不错的呢
记得关注公众号:Python顾木子, 获取完整项目代码哦
边栏推荐
- csdn每日一练——有序表的折半查找
- LabVIEW programming specification
- Classic sentences in Yi Shu's works
- Postgraduate entrance examination English vocabulary unit1
- Is it safe to open an account in Shanghai Stock Exchange?
- Apple CMS collection station source code - building tutorial - attached source code - new source code - development documents
- vtk. VTP download in JS
- PHP实现iframe跨站替换文字/替换iframe网站文字的方法
- Excel essential toolbox 17.0 Free Edition
- Interface test learning notes
猜你喜欢

Halcon combined with C # to detect surface defects -- affine transformation (II)

LabVIEW获取Clamp函数找到的所有点的信息

300 questions on behalf of the first lecture on determinant

LabVIEW uses the visa read function to read USB interrupt data

LabVIEW中创建毫秒时间标识

SystemVerilog(十)-用户自定义类型
![[paper sharing] pata: fuzzing with path aware Taint Analysis](/img/f6/627344c5da588afcf70302ef29d134.png)
[paper sharing] pata: fuzzing with path aware Taint Analysis
![The shell script of pagoda plan task regularly deletes all files under a directory [record]](/img/dc/cfd449bf14c4545cd2e52bda4ab31e.png)
The shell script of pagoda plan task regularly deletes all files under a directory [record]

LabVIEW programming specification

Vs tomato assistant add header comments and usage
随机推荐
LabVIEW pictures look bright or dark after being cast from 16 bits to 8 bits
How to generate automatic references (simple drawings)
Create millisecond time id in LabVIEW
[flutter problem series Chapter 6] how to achieve the scrolling effect of list messages in flutter
[paper sharing] pata: fuzzing with path aware Taint Analysis
LabVIEW change the shape or color of point ROI overlay
【无标题】
Is it safe to open an account in Shanghai Securities?
How to handle the database query error with Emoji expression in Typecho- Xingze V Club
Self made app connected to onenet --- realize data monitoring and distribution control (mqtt)
1. open the R program, and use the apply function to calculate the sum of 1 to 12 in the sequence of every 3 numbers. That is, calculate 1+4+7+10=? 2+5+8+11=?, 3+6+9+12=?
Is it safe to open an account for tongdaxin stock? How to open an account?
Leetcode 501: mode dans l'arbre de recherche binaire
LabVIEW 禁止其他可多核心处理的应用程序在所有核心上执行
Fiddler creates an autoresponder
LabVIEW在波形图或波形图表上显示时间和日期
LabVIEW and VDM extract color and generate gray image
Creating dynamic two-dimensional array with C language
上海网上开户是安全的吗?
After deepin20 menu startup option, the self-test indicates that iwlwwifi is stopped