亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python 模擬登錄B站的示例代碼

瀏覽:4日期:2022-07-02 09:15:46

需要將模擬的瀏覽器,添加到環(huán)境變量中哦。代碼中用的是chrome

from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.common.exceptions import TimeoutExceptionfrom PIL import Imagefrom io import BytesIOfrom time import sleepimport random'''info:author:CriseLYJgithub:https://github.com/CriseLYJ/update_time:2019-3-7'''class BiliBili(): ''' 登陸B(tài)站, 處理驗(yàn)證碼 電腦的縮放比例需要為100%, 否則驗(yàn)證碼圖片的獲取會(huì)出現(xiàn)問(wèn)題 ''' def __init__(self, username, password): ''' 初始化 ''' options = webdriver.ChromeOptions() # 設(shè)置為開(kāi)發(fā)者模式,避免被識(shí)別 options.add_experimental_option(’excludeSwitches’, [’enable-automation’]) self.browser = webdriver.Chrome(options=options) self.url = ’https://passport.bilibili.com/login’ self.browser.get(self.url) self.wait = WebDriverWait(self.browser, 5, 0.2) self.username = username self.password = password def get_button(self): ''' 獲取滑動(dòng)塊, 并且返回 :return: button ''' button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, ’gt_slider_knob’))) return button def get_screenshot(self, button): ''' 獲取網(wǎng)頁(yè)兩次截圖: 1. 鼠標(biāo)懸停于button的截圖 2. 鼠標(biāo)點(diǎn)擊button后的截圖 :param button: 滑動(dòng)塊 :return: 兩次截圖的結(jié)果 ''' ActionChains(self.browser).move_to_element(button).perform() screenshot1 = self.browser.get_screenshot_as_png() screenshot1 = Image.open(BytesIO(screenshot1)) ActionChains(self.browser).click_and_hold(button).perform() screenshot2 = self.browser.get_screenshot_as_png() screenshot2 = Image.open(BytesIO(screenshot2)) return (screenshot1, screenshot2) def get_position(self, button): ''' 獲取驗(yàn)證碼圖片的位置 :return: 位置的四個(gè)點(diǎn)參數(shù) ''' ActionChains(self.browser).move_to_element(button).perform() img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, ’gt_box’))) sleep(2) location = img.location size = img.size print(location, size) top, bottom, left, right = location[’y’], location[’y’] + size[’height’], location[’x’], location[’x’] + size[’width’] return top, bottom, left, right def get_geetest_image(self, button, name1=’captcha1.png’, name2=’captcha2.png’): ''' 獲取兩次驗(yàn)證碼的截圖: 1. 鼠標(biāo)懸停于button的截圖 2. 鼠標(biāo)點(diǎn)擊button后的截圖 :param button: 滑動(dòng)塊 :param name1: 原始驗(yàn)證碼保存的名字 :param name2: 缺塊驗(yàn)證碼保存的名字 :return: 兩次驗(yàn)證碼截圖的結(jié)果 ''' top, bottom, left, right = self.get_position(button) print(’驗(yàn)證碼位置’, top, bottom, left, right) screenshot = self.get_screenshot(button) captcha1 = screenshot[0].crop((left, top, right, bottom)) captcha1.save(name1) captcha2 = screenshot[1].crop((left, top, right, bottom)) captcha2.save(name2) return (captcha1, captcha2) def login(self): ''' 打開(kāi)瀏覽器,并且輸入賬號(hào)密碼 :return: None ''' self.browser.get(self.url) username = self.wait.until(EC.element_to_be_clickable((By.ID, ’login-username’))) password = self.wait.until(EC.element_to_be_clickable((By.ID, ’login-passwd’))) sleep(1) username.send_keys(self.username) sleep(1) password.send_keys(self.password) def is_pixel_equal(self, img1, img2, x, y): ''' 判斷兩個(gè)像素是否相同 :param img1: 原始驗(yàn)證碼 :param img2: 缺塊驗(yàn)證碼 :param x: 像素點(diǎn)的x坐標(biāo) :param y: 像素點(diǎn)的y坐標(biāo) :return: 像素是否相同 ''' pixel1 = img1.load()[x-1, y] pixel2 = img2.load()[x-1, y] threshold = 100 if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs(pixel1[2] - pixel2[2]) < threshold: return True else: return False def get_gap(self, img1, img2): ''' 獲取缺口偏移量 :param img1: 原始驗(yàn)證碼 :param img2: 缺塊驗(yàn)證碼 :return: 第二個(gè)缺塊的左側(cè)的x坐標(biāo) ''' left = 60 # 大致忽略掉第一個(gè)缺塊 for i in range(left, img1.size[0]): for j in range(img1.size[1]):if not self.is_pixel_equal(img1, img2, i, j): left = i return left return left def get_track(self, distance): ''' 獲取滑塊移動(dòng)軌跡的列表 :param distance: 第二個(gè)缺塊的左側(cè)的x坐標(biāo) :return: 滑塊移動(dòng)軌跡列表 ''' track = [] current = 0 mid = distance * 2 / 3 t = 0.2 v = 0 distance += 10 # 使滑塊劃過(guò)目標(biāo)地點(diǎn), 然后回退 while current < distance: if current < mid:a = random.randint(1, 3) else:a = -random.randint(3, 5) v0 = v v = v0 + a * t move = v0 * t + 0.5 * a * t * t current += move track.append(round(move)) for i in range(2): track.append(-random.randint(2, 3)) for i in range(2): track.append(-random.randint(1, 4)) print(track) return track def move_button(self, button, track): ''' 將滑塊拖動(dòng)到指定位置 :param button: 滑動(dòng)塊 :param track: 滑塊運(yùn)動(dòng)軌跡列表 :return: None ''' ActionChains(self.browser).click_and_hold(button).perform() for i in track: ActionChains(self.browser).move_by_offset(xoffset=i, yoffset=0).perform() sleep(0.0005) sleep(0.5) ActionChains(self.browser).release().perform() def crack(self): ''' 串接整個(gè)流程: 1. 輸入賬號(hào)密碼 2. 獲取滑動(dòng)塊 3. 獲取兩張驗(yàn)證碼圖片 4. 獲取滑塊移動(dòng)軌跡 5. 將滑塊拖動(dòng)至指定位置 :return: ''' self.login() button = self.get_button() captcha = self.get_geetest_image(button) left = self.get_gap(captcha[0], captcha[1]) print(left) track = self.get_track(left) # 如果嘗試登陸失敗, 則重新驗(yàn)證, 最多三次 times = 0 while times < 3: self.move_button(button, track) try:success = self.wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME, ’gt_info_type’), ’驗(yàn)證通過(guò):’))print(success) except TimeoutException as e:times += 1print(’fail’) else:print(’success’)return Noneif __name__ == ’__main__’: ACCOUNT = input(’請(qǐng)輸入您的賬號(hào):’) PASSWORD = input(’請(qǐng)輸入您的密碼:’) test = BiliBili(ACCOUNT, PASSWORD) # 輸入賬號(hào)和密碼 test.crack()

以上就是python 模擬登錄B站的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于python 模擬登陸B(tài)站的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: b站 嗶哩嗶哩 Python
相關(guān)文章:
主站蜘蛛池模板: 青青青草国产 | 国产交换精品一区二区三区 | 久久97久久99久久综合 | 欧美r级毛片在线播放 | 欧美一区二区三区免费高 | 亚洲欧美日韩精品久久亚洲区 | www.一级黄色片 | 亚洲午夜大片 | 九九在线偷拍视频在线播放 | 三级福利片 | 九九热精品在线视频 | 欧美 日韩 亚洲另类专区 | 99视频在线精品 | 黄黄的网站在线观看 | 黄色网址大全免费 | 黄色大片一级 | 欧美在线观看日韩欧美在线观看 | 欧美成人黑人xx视频免费观看 | 国产系列欧美系列日韩系列在线 | 大片免免费观看视频播放网站 | 黄色一级片黄色一级片 | 欧美日韩一区二区三区免费 | 白丝丝袜高跟国产在线视频 | 在线视频一区二区 | 国产欧美日韩综合精品无毒 | 欧美黄区 | 国产精品国偷自产在线 | 亚洲午夜免费视频 | 亚洲一区不卡视频 | 一级a性色生活片毛片 | 激情专区| 亚洲色图100p | 日韩三级在线观看 | 一级毛片黄色 | 男女免费高清在线爱做视频 | 广东毛片 | 国产在线一区精品对白麻豆 | 性爽交免费视频 | 97色婷婷| 免费一区二区三区免费视频 | 欧美一级暴毛片 |