当我们收集数据时,我们会访问得太快或太频繁,或者一访问就弹出验证码,然后翻滚珠子。
[En]
When we collect data, we visit it too fast or frequently, or pop up the verification code as soon as we visit, and then clam beads.
今天,我将给你一个简单的方法来处理验证码。
[En]
Today, I will give you a simple way to deal with the CAPTCHA.
; 环境模块
本文使用的是 Python和pycharm
这里需要用到一个 ddddocr 模块 ,这是别人开源写好的一个东西,简单又好用,但是精确度差一点点,但是还是非常好用的。
如果你追求精确度的话,可以调用别人写好的一些API 。
咱们直接 win+r 弹出搜索框后输入 cmd ,点击确定弹出命令提示符窗口, 输入pip install ddddocr 即可安装。
代码展示
代码不多,非常简单。
模块安装完成后,我们先将其导入。
[En]
After the module is installed, let’s import it first.
import ddddocr
然后实例化一下,用一个 cor 接收一下这个数据。
ocr = ddddocr.DdddOcr()
我这里准备了四个验证码
分别实现一下验证码
首先我们用 with open 来读取一下这文件,读取方式使用 rb ,因为是图片的话就读取它的二进制数据
with open('img_3.png', 'rb') as f:
使用 f.read() 将数据读取出来,再自定义一个变量接收一下。
img_bytes = f.read()
然后我们通过 classification 将它传进去,把结果打印出来就可以了。
result = ocr.classification(img_bytes)
print(result)
很多孩子往往因为没有好的学习材料,不知道该学什么知识点,遇到不能及时解决的问题,而未能坚持学习。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Many children often fail to persist in learning because they do not have good learning materials, do not know what knowledge points to learn, and encounter problems that cannot be solved in time.</font>*</details>
我给大家准备了2022最新的Python学习路线图和学习资料、视频教程、电子书等等,都放在这个群里了,还有大佬不定时解答问题,同更多志同道合的伙伴一起学习、一起进步!
python学习交流1群:815624229 (一群已满 加2群)##
Python学习交流2群:279199867 ##
实现效果
纯数字的
字母+数字的
你可以看到,它都是完全识别的,即使上面有一些华丽的水平线或什么。
[En]
You can see that it’s all fully identified, even if there are some gaudy horizontal lines or something on it.
; 完整代码
import ddddocr
ocr = ddddocr.DdddOcr()
with open('img_3.png', 'rb') as f:
img_bytes = f.read()
result = ocr.classification(img_bytes)
print(result)
您可以亲自尝试,也可以直接将其应用于数据收集的实践中。
[En]
You can try it yourself, or you can directly apply it to the practice of data collection.
创作不容易,请帮我订购一套。
[En]
Creation is not easy, please help me to order a collection.
Original: https://www.cnblogs.com/hahaa/p/16411939.html
Author: 轻松学Python
Title: 用Python实现简单的验证码识别
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/499215/
转载文章受原作者版权保护。转载请注明原作者出处!