自动识别验证码登录

任务一

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

def login():
    # 创建浏览器实例
    driver = webdriver.Edge()
    url1 = 'https://login2.scrape.center/'
    driver.get(url1)
    # 输入用户名和密码
    driver.find_element(By.NAME, 'username').send_keys('admin')
    time.sleep(1)
    driver.find_element(By.NAME, 'password').send_keys('admin')
    time.sleep(2)
    # 点击登录按钮
    driver.find_element(By.XPATH, '/html/body/div/div[2]/div/div/div/div/div/form/div[3]/div/input').click()
    time.sleep(2)
    driver.quit()

if __name__ == "__main__":
    login()

任务二

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from PIL import Image,ImageEnhance, ImageFilter
from selenium.webdriver.common.keys import Keys
import io
import base64
import time
import ddddocr
def gray_processing(image):
    # 转为灰度模式
    image = image.convert('L')
    # 灰度化
    image = image.convert("L")
    
    # 增强对比度
    enhancer = ImageEnhance.Contrast(image)
    image = enhancer.enhance(2)  # 调整增强倍数
    
    # 二值化处理
    threshold = 150  # 阈值
    image = image.point(lambda p: p > threshold and 255)
    
    # 平滑处理
    image = image.filter(ImageFilter.SMOOTH)
    
    # 返回预处理后的图像
    return image

def login():
    # 创建浏览器实例
    driver = webdriver.Edge()
    time.sleep(5)
    url2 = 'https://captcha8.scrape.center/'
    driver.get(url2)
    # 输入用户名、密码和验证码
    driver.find_element(By.XPATH, '/html/body/div/div[2]/div/div/div/div/div/form/div[1]/div/div/input').send_keys('admin')
    time.sleep(1)
    driver.find_element(By.XPATH, '/html/body/div/div[2]/div/div/div/div/div/form/div[2]/div/div/input').send_keys('admin')
    time.sleep(1)
    flag = True
    while 1:
        # 执行JavaScript代码来获取canvas元素
        canvas = driver.find_element(By.XPATH, '/html/body/div/div[2]/div/div/div/div/div/form/div[3]/div/div/div[2]/div/canvas')
        canvas_base64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(21);", canvas)
        # 将base64编码的图像数据转换为图像对象
        image_data = io.BytesIO(base64.b64decode(canvas_base64))
        image = Image.open(image_data)
        image = gray_processing(image)
        # 保存图像到本地
        image.save('captcha.png')
        # 识别验证码
        ocr = ddddocr.DdddOcr()  # 实例化对象
        with open('captcha.png', 'rb') as f:
            img_bytes = f.read()  # 读取图片
        code = ocr.classification(img_bytes)  # 识别图片上的字符
        captcha_text = code
        driver.find_element(By.XPATH, '/html/body/div/div[2]/div/div/div/div/div/form/div[3]/div/div/div[1]/div/input').send_keys(captcha_text)
        time.sleep(1)
        # 点击登录按钮
        driver.find_element(By.XPATH, '/html/body/div/div[2]/div/div/div/div/div/form/div[4]/div/button').click()
        time.sleep(1)
        try:
            str1 = driver.find_element(By.XPATH, '//*[@id="app"]/div[2]/div/div/div/div/h2').text
            if str1 == "登录成功": flag = True
        except:
            flag = False
        if flag:
            break
        else:  # 验证未成功就点一下验证码使其刷新
            driver.find_element(By.ID, 'captcha').click()
            time.sleep(0.5)
            inputs = driver.find_element(By.XPATH,'//*[@id="app"]/div[2]/div/div/div/div/div/form/div[3]/div/div/div[''1]/div/input')
            inputs.send_keys(Keys.CONTROL, 'a')
    time.sleep(1)
    driver.quit()
if __name__ == '__main__':
    login()

任务三

由于训练一个识别这种验证码的网络过于复杂,这里使用的是超级鹰的API,在账号密码类型处输入自己的账号密码和类型即可。

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import requests
from PIL import Image
from hashlib import md5

class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
        self.username = username
        password =  password.encode('utf8')
        self.password = md5(password).hexdigest()
        self.soft_id = soft_id
        self.base_params = {
            'user': self.username,
            'pass2': self.password,
            'softid': self.soft_id,
        }
        self.headers = {
            'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
        }

    def PostPic(self, im, codetype):
        """
        im: 图片字节
        codetype: 题目类型 参考 http://www.chaojiying.com/price.html
        """
        params = {
            'codetype': codetype,
        }
        params.update(self.base_params)
        files = {'userfile': ('ccc.jpg', im)}
        r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
        return r.json()

    def PostPic_base64(self, base64_str, codetype):
        """
        im: 图片字节
        codetype: 题目类型 参考 http://www.chaojiying.com/price.html
        """
        params = {
            'codetype': codetype,
            'file_base64':base64_str
        }
        params.update(self.base_params)
        r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, headers=self.headers)
        return r.json()

    def ReportError(self, im_id):
        """
        im_id:报错题目的图片ID
        """
        params = {
            'id': im_id,
        }
        params.update(self.base_params)
        r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
        return r.json()

def get_poses():
    chaojiying = Chaojiying_Client('账号', '密码', '类型')	
    captcha3_new= open('captcha3_new.png', 'rb').read()											
    Pose = chaojiying.PostPic(captcha3_new, 9004)
    Chara = chaojiying.PostPic(captcha3_new, 2004)											
    tip= open('tip.png', 'rb').read()													
    Order =chaojiying.PostPic(tip, 2004)
    Len = len(list(Chara['pic_str']))
    ch = {}
    Pose = Pose['pic_str'].split('|')
    order = []
    for i in range(Len):
        order.append(Order['pic_str'][i])
        ch[Order['pic_str'][i]] = Pose[i]
    clickresult = []
    for i in order:
        clickresult.append(ch[i].split(','))
    return clickresult


def cut_png(img):  # 裁剪图片,去掉多余部分
    im = Image.open(img)
    img_size = im.size
    x = 0
    y = 0
    w = img_size[0]
    h = img_size[1] - 35
    region = im.crop((x, y, x + w, y + h))
    region.save("captcha3_new.png")


def click_img(result, code_img, web_driver):
    x0 = code_img.location['x']  # 验证码图片左上角坐标
    y0 = code_img.location['y']
    for pose in result:
        x = int(pose[0]) + x0
        y = int(pose[1]) + y0
        ActionChains(web_driver).move_by_offset(x, y).click().perform()
        ActionChains(web_driver).move_by_offset(-x, -y).perform()
        time.sleep(0.5)
    # time.sleep(1)



def login():
    
    """登陆程序"""
    url = 'https://captcha3.scrape.center/'
    driver = webdriver.Edge()  # 加载驱动
    driver.get(url)
    driver.implicitly_wait(10)
    # 输入账号
    user = driver.find_element(By.XPATH, '//*[@id="app"]/div[2]/div/div/div/div/div/form/div[1]/div/div/input')
    user.send_keys("admin")
    # 输入密码
    passwd = driver.find_element(By.XPATH, '//*[@id="app"]/div[2]/div/div/div/div/div/form/div[2]/div/div/input')
    passwd.send_keys('admin')
    # 点击登录
    time.sleep(1)
    
    while 1 :
        button = driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div/div/div/div/div/form/div[3]/div/button')
        button.click()
        tip = driver.find_element(By.CLASS_NAME, "geetest_tip_img")
        if(tip):
            break
    flag = 1
    while flag:
        time.sleep(2)
        tip = driver.find_element(By.CLASS_NAME, "geetest_tip_img") # 指明顺序的小图片
        tip.screenshot('tip.png')
        img_code = driver.find_element(By.CLASS_NAME, "geetest_item_img") # 验证码图片
        img_code.screenshot('captcha3.png')
        cut_png('captcha3.png')
        result = get_poses()
        click_img(result, img_code, driver)#点击文字
        time.sleep(2)
        driver.find_element(By.CLASS_NAME, 'geetest_commit_tip').click()       #点击登录
        time.sleep(1)
        str1 = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[6]/div/div/div[2]/div[2]').text  # 通过判断是否出现错误信息来判断成功与否
        if str1 != '验证失败 请按提示重新操作':
            break
        else:   # 失败的话就等三秒让图片刷新然后重新判断
            time.sleep(3)
    time.sleep(10)  # 登陆成功后停留10s


if __name__ == "__main__":
    login()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值