其他分享
首页 > 其他分享> > yidun验证码

yidun验证码

作者:互联网

from PIL import Image
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import cv2
import numpy as np
from io import BytesIO
import time
import requests


class CrackSlider():
    """
    通过浏览器截图,识别验证码中缺口位置,获取需要滑动距离,并模仿人类行为破解滑动验证码
    """     def __init__(self):
        self.url = 'https://id.163yun.com/register?referrer=https://dun.163.com/dashboard&h=yd&t=yd&i18nEnable=true&fromyd=baiduP2_YZM_CP3662'
        self.driver = webdriver.Chrome(executable_path=r'.\chromedriver.exe')
        self.wait = WebDriverWait(self.driver, 20)
        self.zoom = 1     #登录路由
    def open(self):
        self.driver.get(self.url)     #获取注册
    def email_reg(self):
        email_ele = self.wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="m-tab-item"]')))
        email_ele.click()         input_ele = self.wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@class="m-input m-input-large email"]')))
        input_ele.send_keys('455555@qq.com')       #获取图片
    def get_pic(self):
        time.sleep(2)
        target = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'yidun_bg-img')))
        template = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'yidun_jigsaw')))
        target_link = target.get_attribute('src')
        template_link = template.get_attribute('src')
        target_img = Image.open(BytesIO(requests.get(target_link).content))
        template_img = Image.open(BytesIO(requests.get(template_link).content))
        target_img.save('target.jpg')
        template_img.save('template.png')
        local_img = Image.open('target.jpg')
        size_loc = local_img.size
        self.zoom = 320 / int(size_loc[0])     #移动速度
    def get_tracks(self, distance):
        print(distance)
        distance += 20
        v = 0
        t = 0.2
        forward_tracks = []
        current = 0
        mid = distance * 3 / 5  #减速阀值
        while current < distance:
            if current < mid:
                a = 2  #加速度为+2
            else:
                a = -3  #加速度-3
            s  = v * t + 0.5 * a * (t ** 2)
            v = v + a * t
            current += s
            forward_tracks.append(round(s))         back_tracks = [-3, -3, -2, -2, -2, -2, -2, -1, -1,-1,-1]
        return {'forward_tracks': forward_tracks, 'back_tracks': back_tracks}     #坐标处理
    def match(self, target, template):
        img_rgb = cv2.imread(target)
        img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
        template = cv2.imread(template, 0)
        run = 1
        w, h = template.shape[::-1]
        print(w, h)
        res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
        run = 1         # 使用二分法查找阈值的精确值
        L = 0
        R = 1
        while run < 20:
            run += 1
            threshold = (R + L) / 2
            print(threshold)
            if threshold < 0:
                print('Error')
                return None
            loc = np.where(res >= threshold)
            print(len(loc[1]))
            if len(loc[1]) > 1:
                L += (R - L) / 2
            elif len(loc[1]) == 1:
                print('目标区域起点x坐标为:%d' % loc[1][0])
                break
            elif len(loc[1]) < 1:
                R -= (R - L) / 2
        return loc[1][0]
     #移动模拟手速
    def crack_slider(self):
        slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'yidun_slider')))
        ActionChains(self.driver).click_and_hold(slider).perform()         for track in self.tracks['forward_tracks']:
            ActionChains(self.driver).move_by_offset(xoffset=track, yoffset=0).perform()         time.sleep(0.5)
        for back_tracks in self.tracks['back_tracks']:
            ActionChains(self.driver).move_by_offset(xoffset=back_tracks, yoffset=0).perform()         ActionChains(self.driver).move_by_offset(xoffset=-4, yoffset=0).perform()
        ActionChains(self.driver).move_by_offset(xoffset=9.7, yoffset=0).perform()
        time.sleep(0.5)         ActionChains(self.driver).release().perform()     #获取点击
    def button_m(self):
        pwd = self.wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@class="m-input m-input-large password"]')))
        pwd.send_keys('123qwe')
        but=self.wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="bg"]/div[2]/div/div/div/div/div/form/div[4]/button')))
        but.click()     #起步操作
    def start(self):
        self.open()
        self.email_reg()
        target = 'target.jpg'
        template = 'template.png'
        self.get_pic()
        distance = self.match(target, template)
        self.tracks = self.get_tracks((distance + 7) * self.zoom)  # 对位移的缩放计算
        self.crack_slider()
        time.sleep(1)
        self.button_m() if __name__ == '__main__':
    cs = CrackSlider()
    cs.start()                                                                                                

标签:target,img,get,self,yidun,验证码,tracks,template
来源: https://www.cnblogs.com/xdlzs/p/10650331.html