当前位置:   article > 正文

极验点字验证码登陆

点字验证码

雪球网

from selenium import webdriver
from selenium.webdriver import ActionChains
from io import BytesIO
import json
import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from PIL import Image
from lianzhong.lianzong_api import Lianzong_api
jsdati = Lianzong_api('账户', '密码')

USERNAME = '1'
PASSWORD = 'h'
class Login(object):
    def __init__(self):
        self.url = 'https://xueqiu.com/'
        self.browser = webdriver.Chrome()
        self.username = USERNAME
        self.password = PASSWORD
        self.wait = WebDriverWait(self.browser, 20)

    def __del__(self):
        print("close")


    def open(self):
        self.browser.get(self.url)
        self.browser.find_element_by_xpath("//div[@class='nav__login__btn']").click()
        username = self.wait.until(EC.presence_of_element_located((By.XPATH, '//input[@name="username"]')))
        pwd = self.wait.until(EC.presence_of_element_located((By.XPATH, '//input[@name="password"]')))
        username.send_keys(self.username)
        time.sleep(2)
        pwd.send_keys(self.password)

    # 获取验证码按钮
    def get_yzm_button(self):

        button = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'modal__login__btn')))
        return button

    # 获取验证码图片对象
    def get_img_element(self):
        element = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_widget')))
        return element


# 不使用
    def get_position(self):

       #   获取验证码位置

        element = self.get_img_element()
        sleep(2)
        location = element.location
        size = element.size
        top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[
            'width']
        return left, top, right, bottom


    # 获取网页截图
    def get_screen_shot(self):
        screen_shot = self.browser.get_screenshot_as_png()
        screen_shot = Image.open(BytesIO(screen_shot))
        return screen_shot


    def get_yzm_img(self, name='captcha.png'):
       #  获取验证码图片
        left, top, right, bottom = self.get_position()
        screen_shot = self.get_screen_shot()
        captcha = screen_shot.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:
            ActionChains(self.browser).move_to_element_with_offset(self.get_img_element(), location[0],
                                                                   location[1]).click().perform()
            time.sleep(1)


    def touch_click_verify(self):
        """
        点击登陆按钮
        :return: None
        """
        button = self.browser.find_element_by_xpath('//a[@class="geetest_commit"]/div')
        button.click()


    def get_cookies(self):
        try:
            cookie_list = self.browser.get_cookies()
            print("cookie_list", cookie_list)
            # cookie_dict = {}
            # for cookie in cookie_list:
            #     if 'name' in cookie and 'value' in cookie:
            #         cookie_dict[cookie['name']] = cookie['value']
            cookie_dict = {i['name']: i['value'] for i in cookie_list}
            print('cookie_dict:', cookie_dict)
            with open('xueqiu_cookies', 'w', encoding='utf8')as f:
                cookie_dict = json.dumps(cookie_dict)
                f.write(cookie_dict)
            return cookie_dict
        except:
            print("cookie 获取失败")
            return None

# 读取cookie
    def return_cookie(self):
        cookies = ''
        with open('xueqiu_cookies', 'r')as f:
            cookie = f.read()[1:-1]
            cookie = cookie.split(', ')
            for i in cookie:
                cook = i.split(': ')
                cookies += cook[0][1:-1] + '=' + cook[1][1:-1] + ';'
            print('cookies',cookies)
            return cookies


    def run(self): # 破解入口
        self.open(), sleep(2)
        self.get_yzm_button().click(), sleep(2)# 点击验证按钮
        image = self.get_yzm_img("xq.png")
        bytes_array = BytesIO()
        image.save(bytes_array, format='PNG')# 获取验证码图片
        result = jsdati.decode(os.path.join(os.path.dirname(__file__), 'xq.png'))# 识别验证码
        print(result)
        self.touch_click_words(result)
        self.touch_click_verify(), time.sleep(3)
        # 判定是否成功
        try:
            elem = self.wait.until(
                # EC.text_to_be_present_in_element((By.CLASS_NAME, 'nav__btn--longtext'), '发帖'))
                EC.presence_of_element_located((By.CLASS_NAME, 'nav__login__btn')))
            if len(elem.text) != 0:
                print(elem.text, 'fail!')
                time.sleep(3)
                self.run()
            else:
                print(1)
        except Exception as e:
            print(e, 'success! ', end='')
            self.get_cookies()
            time.sleep(3)
            # cookie = self.cookies()
            # return cookie


if __name__ == '__main__':
    crack = Login()
    crack.run()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175

联众接入

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/527725
推荐阅读
相关标签
  

闽ICP备14008679号