#!/usr/bin/python
# !coding=utf-8
import os
import time
from io import BytesIO
import tesserocr
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "http://my.cnki.net/elibregister/commonregister.aspx"
username = "e7as711350"
password = "testtest@339"
email = "te421g44@163.com"
captcha_name = "captcha.png"
threshold = 100
class CrackCnki(object):
def __init__(self):
self.username = username
self.password = password
self.email = email
self.browser = webdriver.Chrome()
self.wait = WebDriverWait(self.browser, 5)
def __del__(self):
self.browser.close()
def open_borwser(self):
self.browser.get(url)
def fill_form(self):
input_user = self.browser.find_element_by_id("username")
input_user.send_keys(self.username)
input_password = self.browser.find_element_by_id("txtPassword")
input_password.send_keys(self.password)
input_email = self.browser.find_element_by_id("txtEmail")
input_email.send_keys(self.email)
def get_img_position(self):
img = self.wait.until(EC.presence_of_element_located((By.ID, "checkcode")))
time.sleep(2)
location = img.location
size = img.size
top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[
'width']
return (top, bottom, left, right)
def get_screenshot(self):
screenshot = self.browser.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
return screenshot
def get_images(self):
if os.path.exists(captcha_name):
os.remove(captcha_name)
top, bottom, left, right = self.get_img_position()
screenshot = self.get_screenshot()
captcha = screenshot.crop((left, top, right, bottom))
captcha.save(captcha_name)
def get_captcha(self):
image = Image.open(captcha_name)
image = image.convert("L")
# threshold = self.threshold
# table = []
# for i in range(256):
# if i < threshold:
# table.append(0)
# else:
# table.append(1)
# image = image.point(table, "1")
result = tesserocr.image_to_text(image)
return result
def fill_captcha(self):
input_captcha = self.browser.find_element_by_id("txtOldCheckCode")
captcha_txt = self.get_captcha()
input_captcha.send_keys(captcha_txt)
def change_img(self):
button = self.wait.until(EC.element_to_be_clickable((By.ID, "checkcode")))
button.click()
input_captcha = self.browser.find_element_by_id("txtOldCheckCode")
input_captcha.clear()
def check_captcha(self):
self.get_img_position()
self.get_images()
self.fill_captcha()
def chick_button(self):
button = self.wait.until(EC.element_to_be_clickable((By.ID, "ButtonRegister")))
button.click()
def check_login(self):
try:
success = self.wait.until(
EC.text_to_be_present_in_element((By.ID, "span_oldcheckcode"), "验证码不正确,请重新输入"))
if success is True:
self.change_img()
self.check_captcha()
self.chick_button()
self.check_login()
except:
print("用户:%s 密码:%s ,注册成功!" % (self.username, self.password))
if __name__ == "__main__":
crack = CrackCnki()
crack.open_borwser()
crack.fill_form()
crack.check_captcha()
crack.chick_button()
crack.check_login()
python循环注册识别验证码
最新推荐文章于 2024-02-19 15:34:45 发布