Example #1
0
    # 浏览器页面截屏
    driver.get_screenshot_as_file(screenImg)
    # 定位验证码位置及大小
    location = driver.find_element_by_name('authImage').location
    size = driver.find_element_by_name('authImage').size
       # 下面四行我都在后面加了数字,理论上是不用加的,但是不加我这截的不是验证码那一块的图,可以看保存的截图,根据截图修改截图位置
    left = location['x'] + 530
    top = location['y'] + 175
    right = location['x'] + size['width'] + 553
    bottom = location['y'] + size['height'] + 200
    # 从文件读取截图,截取验证码位置再次保存
    img = Image.open(screenImg).crop((left, top, right, bottom))
       # 下面对图片做了一些处理,能更好识别一些,相关处理再百度看吧
    img = img.convert('RGBA')  # 转换模式:L | RGB
    img = img.convert('L')  # 转换模式:L | RGB
    img = Image.Contrast(img)  # 增强对比度
    img = img.enhance(2.0)  # 增加饱和度
    img.save(screenImg)
    # 再次读取识别验证码
    img = Image.open(screenImg)
    code = pytesseract.image_to_string(img)
    # 打印识别的验证码
    # print(code.strip())

    # 识别出来验证码去特殊符号,用到了正则表达式,这是我第一次用,之前也没研究过,所以用的可能粗糙,请见谅
    b = ''
    for i in code.strip():
        pattern = re.compile(r'[a-zA-Z0-9]')
        m = pattern.search(i)
        if m != None:
            b += i