1. 程式人生 > >微博驗證碼的識別並登入獲取cookies

微博驗證碼的識別並登入獲取cookies

記得以前微博是用的宮格驗證碼,現在宮格已經被棄用了,用的是滑塊驗證碼和 點觸驗證碼,每天登陸的第一次基本用的是滑塊,繼續登入就都用的是點觸驗證碼。所以滑塊驗證碼不寫,感興趣的可以補上。

程式碼:

這裡用的超級鷹的打碼平臺,要去超級鷹下載對應的API,並按提示修改,順便購買一些題分,很便宜。

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from PIL import Image from io import BytesIO from selenium.webdriver import ActionChains import time from requests import Session from weibo.chaojiying import Chaojiying_Client from selenium.common.exceptions import TimeoutException from weibo.crack import Crack EMAIL='1819241****'#微博賬號 PASSWORD='*******'#微博密碼 # 超級鷹使用者名稱,密碼,軟體id,驗證碼型別 CHAOJIYING_USERNAME='1819241****' CHAOJIYING_PASSWORD='********' CHAOJIYING_SOFT_ID=902020 CHAOJIYING_KIND=9004 class weibo_click(): def __init__(self,username,password): self.url = "https://passport.weibo.cn/signin/login" self.email = username self.password = password self.browser = webdriver.Chrome() # self.get=self self.wait = WebDriverWait(self.browser, 15) self.chaojiying = Chaojiying_Client(CHAOJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAOJIYING_SOFT_ID) def open(self): email = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#loginName"))) password = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#loginPassword"))) email.send_keys(self.email) time.sleep(2) password.send_keys(self.password) time.sleep(2) def login(self): submit = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn"))) submit.click() # time.sleep(1) # print("登陸成功!") def get_geetest_button(self): button = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.geetest_radar_tip"))) button.click() def go(self): self.browser.get(self.url) self.browser.maximize_window() self.open() time.sleep(1) self.login() # self.get_geetest_button() def get_screenshot(self): # 截圖功能 screenshot = self.browser.get_screenshot_as_png() screenshot = Image.open(BytesIO(screenshot)) return screenshot def get_position(self): ''' 獲取點觸驗證碼位置 :return: 點觸驗證碼位置元組 ''' img=self.get_weibo_element() # img =self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, "geetest_item_img "))) time.sleep(1) location = img.location size = img.size print(location) print(size) top, bottom, left, right = location["y"], location["y"] + size["height"], location["x"], \ location["x"] + size["width"] return (top, bottom, left, right) def click_image(self): ''' 點觸驗證碼截圖 :return: 點觸驗證碼物件 ''' top, bottom, left, right = self.get_position() print("驗證碼位置", top, bottom, left, right) screenshot = self.get_screenshot() # screenshot.show() print('大圖尺寸,', screenshot.size) captcha = screenshot.crop((833, 200, 1252, 632)) # crop方法是Image裡的截圖方法 先後順序有規定(左,上,右,下) captcha.save('bbb.png') # captcha.show() print('小圖尺寸,', captcha.size) # img = Image.open(name) (x, y) = captcha.size x_s = 334 y_s = round(y * x_s / x) out_img = captcha.resize((x_s, y_s), Image.ANTIALIAS) out_img.save('ccc.png') # out_img.show() return out_img def result(self): image = self.click_image() bytes_array = BytesIO() image.save(bytes_array, format='PNG') # 識別驗證碼 result = self.chaojiying.PostPic(bytes_array.getvalue(), CHAOJIYING_KIND) return result 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] print(locations) return locations def get_weibo_element(self): ''' 獲取點觸驗證碼物件 :return: 圖片物件 ''' element = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, "geetest_item_img "))) return element def weibo_click_words(self,locations): ''' 點選驗證碼圖片 :param locations: 點選位置 :return: None ''' for location in locations: print(location) ActionChains(self.browser).move_to_element_with_offset(self.get_weibo_element(),location[0],location[1]).click().perform() time.sleep(1) def confirm(self): push=self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.geetest_commit_tip"))) push.click() def main(self): print('點觸驗證碼的驗證') while True: captcha_result = self.result() locations = self.get_points(captcha_result) self.weibo_click_words(locations) button = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.geetest_commit_tip"))) button.click() if self.login_successfully(): break else: continue def password_error(self): """ 判斷是否密碼錯誤 :return: """ try: return WebDriverWait(self.browser, 5).until( EC.text_to_be_present_in_element((By.ID, 'errorMsg'), '使用者名稱或密碼錯誤')) except TimeoutException: return False def login_successfully(self): """ 判斷是否登入成功 :return: """ try: return bool( WebDriverWait(self.browser, 5).until( EC.presence_of_element_located((By.CSS_SELECTOR, "div.m-text-cut")))) except TimeoutException: return False def get_cookies(self): """ 獲取Cookies :return: """ return self.browser.get_cookies() def man(self): """ 破解入口 :return: """ self.go() # 驗證賬號和密碼 if self.password_error(): return { 'status': 2, 'content': '使用者名稱或密碼錯誤' }

# 賬號密碼沒錯就獲取圖片物件 self.get_geetest_button() self.main() # 判斷是否登入成功 if self.login_successfully(): cookies = self.get_cookies() print('cookies',cookies) return { 'status': 1, 'content': cookies } else: return { 'status': 3, 'content': '登入失敗' } if __name__=="__main__":
result=weibo_click(EMAIL,PASSWORD).man()
print(result.get('status'))

 

&n