赞
踩
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()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。