python_字母/数字验证码获取

本文介绍了如何利用Python的ddddocr库结合selenium的ChromeWebDriver来自动化登录网站并识别验证码。通过截取网页中的验证码图片,然后用ddddocr进行识别,可以实现对数字/字母验证码的自动处理。
摘要由CSDN通过智能技术生成

前言

  • 在使用自动化登录网站时,经常输入用户名和密码后就会遇到验证码,今天介绍一款通用验证码识别 OCR库,它的名字是ddddocr。

一、安装

pip install ddddocr

二、使用ddddocr

获取数字/字母验证码

在这里插入图片描述

  • 方法一
import os
import ddddocr
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By


class GetVerificationCode:

    def __init__(self):
        # 创建了一个Chrome浏览器的WebDriver实例
        self.drive = webdriver.Chrome()
        # 最大化窗口
        self.drive.maximize_window()
        # 隐式等待,设置最大的等待时长,只对查找元素(find_elementXXX)生效
        self.drive.implicitly_wait(2)

    def getverification(self, location):
        '''获取验证码信息'''
        # 获取当前文件的位置
        current_location = os.path.dirname(__file__)

        # 截取当前网页并放到指定目录下,并命名为printscreen,该截图中有我们需要的验证码
        self.drive.save_screenshot(f'{current_location}\\printscreen.png')

        # 定位验证码位置
        position = self.drive.find_element(By.CSS_SELECTOR, location)

        # 获取验证码x, y轴坐标
        location = position.location
        # 获取验证码的宽度、高度
        size = position.size

        # 需要截取的位置坐标
        rangle = (int(location['x']), int(location['y']),
                  int(location['x'] + size['width']),
                  int(location['y'] + size['height']))

        # 打开截图
        i = Image.open(f'{current_location}\\printscreen.png')
        # 使用Image的crop函数,从截图中再次截取我们需要的区域
        fimg = i.crop(rangle)
        fimg = fimg.convert('RGB')
        # 保存我们截下来的验证码图片,并读取验证码内容
        fimg.save(f'{current_location}\\code.png')

        # 获取验证码
        ocr = ddddocr.DdddOcr()
        with open(f'{current_location}\\code.png', 'rb') as f:
            img_bytes = f.read()
        self.res = ocr.classification(img_bytes)
        print('识别出的验证码为:' + self.res)

    def login(self):
        '''登录'''
        # 打开网站
        self.drive.get('需要获取验证码的网站')
        # 获取验证码
        self.getverification('.bgw')


if __name__ == '__main__':
    run = GetVerificationCode()
    run.login()
  • 方法二
import os
import ddddocr
from selenium import webdriver
from selenium.webdriver.common.by import By


class GetVerificationCode:

    def __init__(self):
        # 创建了一个Chrome浏览器的WebDriver实例
        self.drive = webdriver.Chrome()
        # 最大化窗口
        self.drive.maximize_window()
        # 隐式等待,设置最大的等待时长,只对查找元素(find_elementXXX)生效
        self.drive.implicitly_wait(2)

    def getVerification(self, location):
        '''获取验证码信息'''
        # 获取当前文件的位置
        current_location = os.path.dirname(__file__)

        # 定位验证码位置
        position = self.drive.find_element(By.CSS_SELECTOR, location)

        # 截取页面上固定元素的图片(验证码)
        position.screenshot(f'{current_location}\\code.png')

        # 获取验证码
        ocr = ddddocr.DdddOcr()
        with open(f'{current_location}\\code.png', 'rb') as f:
            img_bytes = f.read()
        self.res = ocr.classification(img_bytes)
        print('识别出的验证码为:' + self.res)

    def login(self):
        '''登录'''
        # 打开网站
        self.drive.get('需要获取验证码的网站')
        # 获取验证码
        self.getVerification('.bgw')


if __name__ == '__main__':
    run = GetVerificationCode()
    run.login()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值