小白都能轻松掌握,python最稳定的图片识别库ddddocr

🚁前言

在爬虫过程中,大多我们都会碰到验证码识别,它是常用的一种反爬手段,包括:滑块验证码,图片验证码,算术验证码,点击验证码,所讲的图片验证码是较简单的,因为有大佬,给我们造好了轮子,我们直接套用就行!

🚁测试

🚁对比Pytesseract

这是题外的,为什么要做对比呢,有对比才能知道他的优缺点。

  • 安装pytesseract
pip install pytesseract
  • 准备
    在这里插入图片描述
def get_captcha():

    image = Image.open('VerifyCode.png')
    image = image.convert('L')  # 灰度处理

    threshold = 220 # 阈值,二值化处理
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    image = image.point(table, '1')
    image.show()
    ans = pytesseract.image_to_string(image)
    print(ans)

get_captcha()
  • 结果
    这是处理过的图片
    在这里插入图片描述
    在这里插入图片描述
    发现差别还是有点大的,要是没有训练模型,生产使用确实不太行!

🚁使用ddddocr

🚁简介

硬性要求

python >= 3.8

安装

pip install ddddocr

GitHub
文档地址

测试,还是刚刚那种图。

import ddddocr

def recognize():
    ocr = ddddocr.DdddOcr()
    with open('code_img/VerifyCode.png', 'rb') as f:
        img_bytes = f.read()
    res = ocr.classification(img_bytes)
    print(res)

recognize()

结果,一眼可见,没有对比就没有伤害
在这里插入图片描述
短短5行代码,就饶过了图片验证,是不是觉得很爽!

🚁实战

利用Amazon的机器人验证,帮助我们绕过反爬,获取我们所要的数据
在这里插入图片描述

from selenium import webdriver
from selenium.webdriver import ChromeOptions
from io import BytesIO
import time
from ocr_code import recognize
from PIL import Image

options = ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument("disable-blink-features=AutomationControlled")
options.add_argument(
    'User-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36')

url = 'https://www.amazon.com/errors/validateCaptcha'
browser = webdriver.Chrome('chromedriver.exe', options=options)

def getCookie():
    browser.set_window_size(1920, 1080)
    browser.get(url)
    time.sleep(1)

    '''
    /处理验证码
    '''
    # 要截图的元素
     try:
         element = browser.find_element_by_xpath('//div[@class="a-row a-text-center"]')
         # 坐标
         x, y = element.location.values()
         # 宽高
         h, w = element.size.values()
         # 把截图以二进制形式的数据返回
         image_data = browser.get_screenshot_as_png()
         # 以新图片打开返回的数据
         screenshot = Image.open(BytesIO(image_data))
         # 对截图进行裁剪
         result = screenshot.crop((x, y, x + w, y + h))
         # 显示图片
         # result.show()
         # 保存验证码图片
         result.save('VerifyCode.png')
         # 调用recognize方法识别验证码
         code = recognize('VerifyCode.png')
         print(code)
         # 输入验证码
         browser.find_element_by_name('field-keywords').send_keys(code)
         # 点击确认
         browser.find_element_by_class_name('a-button-text').click()
         time.sleep(1)
     except:
         break


if __name__ == '__main__':
    getCookie()

ocr_code.py

import ddddocr

def recognize(image):
    ocr = ddddocr.DdddOcr()
    with open(image, 'rb') as f:
        img_bytes = f.read()
    res = ocr.classification(img_bytes)
    return res


🚁成果

截取的验证码

在这里插入图片描述
打印

在这里插入图片描述

点关注不迷路,本文章若对你有帮助,烦请三连支持一下 ❤️❤️❤️
各位的支持和认可就是我最大的动力❤️❤️❤️

在这里插入图片描述

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年3月5日 下午9:13
下一篇 2023年3月5日 下午9:14

相关推荐