美文网首页
使用超级鹰打码平台处理登录的文字点击验证码-----已简书自动登

使用超级鹰打码平台处理登录的文字点击验证码-----已简书自动登

作者: Py_Explorer | 来源:发表于2018-09-20 11:25 被阅读0次

    开发使用工具pycharm,win10;版本 py2.7+

    1. 首先注册超级鹰的会员

    2. 直接看代码吧

    #超级鹰平台处理代码
    #coding=utf-8
    import requests
    from hashlib import md5
    
    
    class Chaojiying(object):
        def __init__(self, username, password, soft_id):
            self.username = username
            self.password = md5(password.encode('utf-8')).hexdigest()
            self.soft_id = soft_id
            self.base_params = {
                'user': self.username,      #用户名
                'pass2': self.password,     #用户密码
                'softid': self.soft_id,     #软件ID,在用户中心->软件ID->自己生成
            }
            self.headers = {
                'Connection': 'Keep-Alive',
                'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
            }
    
        def post_pic(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 report_error(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()
    

    登录页面代码

    #coding=utf-8
    import time
    from io import BytesIO
    from PIL import Image
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from chaojiying import Chaojiying
    #为简书的用户账号和密码
    EMAIL = '**********'
    PASSWORD = '*******'
    #此四者分别为,超级鹰的用户名,密码,软件ID,和验证码类型
    CHAOJIYING_USERNAME = '*****'
    CHAOJIYING_PASSWORD = '****'
    CHAOJIYING_SOFT_ID = ****
    #9004为:坐标多选,返回1~4个坐标,如:x1,y1|x2,y2|x3,y3
    CHAOJIYING_KIND = 9004
    
    
    class CrackTouClick():
        def __init__(self):
            self.url = 'https://www.jianshu.com/sign_in'
            self.browser = webdriver.Chrome()
            self.wait = WebDriverWait(self.browser, 20)
            self.email = EMAIL
            self.password = PASSWORD
            self.chaojiying = Chaojiying(CHAOJIYING_USERNAME,     CHAOJIYING_PASSWORD, CHAOJIYING_SOFT_ID)
    
        def __del__(self):
            self.browser.close()
    
        def open(self):
            """
            打开网页输入用户名密码
            :return: None
            """
            self.browser.get(self.url)
            email = self.wait.until(EC.presence_of_element_located((By.ID, 'session_email_or_mobile_number')))
            password = self.wait.until(EC.presence_of_element_located((By.ID, 'session_password')))
            email.send_keys(self.email)
            password.send_keys(self.password)
    
        def get_touclick_button(self):
            """
            获取初始验证按钮
            :return:
            """
            button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sign-in-button')))
            return button
    
        def get_touclick_element(self):
            """
            获取验证图片对象
            :return: 图片对象
            """
            element = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_widget')))
            return element
    
        def get_position(self):
            """
            获取验证码位置
            :return: 验证码位置元组
            """
            element = self.get_touclick_element()
            time.sleep(2)
            location = element.location
            size = element.size
            top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[
            'width']
            top = int(top)
            bottom = int(bottom)
            left = int(left)
            right = int(right)
            return (top, bottom, left, right)
    
        def get_screenshot(self):
            """
            获取网页截图
            :return: 截图对象
            """
            screenshot = self.browser.get_screenshot_as_png()
            screenshot = Image.open(BytesIO(screenshot))
           return screenshot
    
        def get_touclick_image(self, name='captch.png'):
            """
            获取验证码图片
            :return: 图片对象
            """
            top, bottom, left, right = self.get_position()
            print('验证码位置', top, bottom, left, right)
            screenshot = self.get_screenshot()
            captcha = screenshot.crop((left, top, right, bottom))
            captcha.save(name)
            return captcha
    
        def get_points(self, captcha_result):
            """
            解析识别结果
            :param captcha_result: 识别结果
            :return: 转化后的结果
            """
            groups = captcha_result.get('pic_str').split('|')
            locations = [[int(number) for number in group.split(',')] for group in groups]
            return locations
    
        def touch_click_words(self, locations):
            """
            点击验证图片
            :param locations: 点击位置
            :return: None
            """
            for location in locations:
                print(location)
                ActionChains(self.browser).move_to_element_with_offset(self.get_touclick_element(), location[0],
                                                                   location[1]).click().perform()
                time.sleep(1)
    
        def touch_click_verify(self):
            """
            点击验证按钮
            :return: None
            """
            button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_commit')))
            button.click()
    
        def login(self):
            """
            登录
            :return: None
            """
            submit = self.wait.until(EC.element_to_be_clickable((By.ID, '_submit')))
            submit.click()
            time.sleep(10)
            print('登录成功')
    
        def crack(self):
            """
            破解入口
            :return: None
            """
            self.open()
            # 点击验证按钮
            button = self.get_touclick_button()
            button.click()
            # 获取验证码图片
            image = self.get_touclick_image()
            bytes_array = BytesIO()
            image.save(bytes_array, format='PNG')
            # 识别验证码
            result = self.chaojiying.post_pic(bytes_array.getvalue(), CHAOJIYING_KIND)
            print(result)
            locations = self.get_points(result)
            self.touch_click_words(locations)
            self.touch_click_verify()
            # 判定是否成功
            # time.sleep(10)
            try:
                success = self.wait.until(
                    EC.text_to_be_present_in_element((By.CLASS_NAME, 'btn write-btn'), '验证成功'))
                print(success)
            except:
                # 失败后重试
                # if not success:
                self.crack()
                # else:
                #     self.login()
    
    
    if __name__ == '__main__':
        crack = CrackTouClick()
        crack.crack()
    

    以上就是完整的两部分代码,如遇到问题,可留言。

    相关文章

      网友评论

          本文标题:使用超级鹰打码平台处理登录的文字点击验证码-----已简书自动登

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