美文网首页
机器学习 破解验证码

机器学习 破解验证码

作者: 站长_郭生 | 来源:发表于2018-08-21 15:45 被阅读0次

安装 tesseract 库

利用tesseract 训练数据。最终识别验证码。

先识别简单的验证码 数据包用chi_sim。 (复杂验证码需要训练)

破解验证码: image3.jpeg
image6.jpeg
# -*- coding: utf-8 -*-

from selenium import webdriver
import pytesseract
from PIL import Image
import re
import redis

cookie_name = "m_"
username = "xxxxxx"
password = "xxxxxx"
login_url = "http://xxx/"

# 像素识别
def binarizing(img, threshold):
    pixdata = img.load()
    w, h = img.size
    for y in range(h):
        for x in range(w):
            if pixdata[x, y] < threshold:
                pixdata[x, y] = 0
            else:
                pixdata[x, y] = 255
    return img


def getCode():
    # open image
    image = Image.open('a.png')
    # image.show()
    ic = image.convert("L")
    i = binarizing(ic, 85)
    # i.show()
    code = pytesseract.image_to_string(i, lang='chi_sim')
    nums = re.findall(r"\d+\.?\d*", code)

    print(nums)
    if len(nums) == 1:
        result = int(nums[0])
    elif len(nums) >= 2:
        result = int(nums[0]) + int(nums[1])
    else:
        result = 0

    return result

browser = webdriver.Firefox(executable_path="/Users/guoxingjun/Downloads/geckodriver")

browser.set_page_load_timeout(1)
browser.set_script_timeout(1)

browser.delete_all_cookies()

browser.maximize_window()  # 浏览器最大化
try:
    browser.get(login_url)  # 该处为具体网址
except:
    browser.close()

browser.implicitly_wait(1)

browser.get_screenshot_as_file('a.png')
location = browser.find_element_by_class_name('info_yanzhen').location
size = browser.find_element_by_class_name('info_yanzhen').size
left = location['x'] * 2
top = location['y'] * 2
right = location['x'] * 2 + size['width'] * 2 + 20
bottom = location['y'] * 2 + size['height'] * 2 + 20
a = Image.open("a.png")
im = a.crop((left, top, right, bottom))
im.save('a.png')
#

# 图片转换成字符
vcode = getCode()
print(vcode)
# 填充用户名 密码 验证码
browser.find_element_by_name("username").send_keys(username)
browser.find_element_by_name("password").send_keys(password)
browser.find_element_by_name("confirmationCode").send_keys(vcode)
# 点击登录
browser.find_element_by_id("btnSubmit").click()

browser.implicitly_wait(2)
print(browser.find_element_by_id("btnSubmit").text)

cookie = {}
for i in browser.get_cookies():
    cookie[cookie_name + i["name"]] = i["value"]
browser.implicitly_wait(1)
browser.quit()

相关文章

网友评论

      本文标题:机器学习 破解验证码

      本文链接:https://www.haomeiwen.com/subject/drljiftx.html