生活随笔
收集整理的這篇文章主要介紹了
使用打码平台登录B站
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
B站登錄需要用到圖片點坐標點選類型的驗證碼驗證,使用傳統(tǒng)的OCR技術(shù)解決起來比較棘手,因此借助于第三方打碼平臺,可以快速實現(xiàn)這一功能。
- 創(chuàng)建項目【使用打碼平臺登錄B站】,新建python文件【bili.py】【img_api.py】【accounts.py】
- 進入【accounts.py】,定義你的打碼平臺和B站賬號以及密碼
"""快識別賬號密碼"""
KUAI_USER = '你的賬號'
KUAI_PASS = '你的密碼'"""B站賬號密碼"""
BILI_USER = '你的賬號'
BILI_PASS = '你的密碼'
- 進入【img_api.py】文件,進入打碼平臺開發(fā)文檔的python接口(上面有提示,不打廣告),把開發(fā)文檔復(fù)制到【img_api.py】
import base64
import json
import requests
# 一、圖片文字類型(默認 3 數(shù)英混合):
# 1 : 純數(shù)字
# 1001:純數(shù)字2
# 2 : 純英文
# 1002:純英文2
# 3 : 數(shù)英混合
# 1003:數(shù)英混合2
# 4 : 閃動GIF
# 7 : 無感學(xué)習(xí)(獨家)
# 11 : 計算題
# 1005: 快速計算題
# 16 : 漢字
# 32 : 通用文字識別(證件、單據(jù))
# 66: 問答題
# 49 :recaptcha圖片識別
# 二、圖片旋轉(zhuǎn)角度類型:
# 29 : 旋轉(zhuǎn)類型
#
# 三、圖片坐標點選類型:
# 19 : 1個坐標
# 20 : 3個坐標
# 21 : 3 ~ 5個坐標
# 22 : 5 ~ 8個坐標
# 27 : 1 ~ 4個坐標
# 48 : 軌跡類型
#
# 四、缺口識別
# 18 : 缺口識別(需要2張圖 一張目標圖一張缺口圖)
# 33 : 單缺口識別(返回X軸坐標 只需要1張圖)
# 五、拼圖識別
# 53:拼圖識別
def base64_api(uname, pwd, img, typeid):with open(img, 'rb') as f:base64_data = base64.b64encode(f.read())b64 = base64_data.decode()data = {"username": uname, "password": pwd, "typeid": typeid, "image": b64}result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)if result['success']:return result["data"]["result"]else:return result["message"]return ""if __name__ == "__main__":img_path = "C:/Users/Administrator/Desktop/file.jpg"result = base64_api(uname='你的賬號', pwd='你的密碼', img=img_path, typeid=3)print(result)
import base64
from accounts import KUAI_USER, KUAI_PASS # accounts.py中導(dǎo)入你定義的打碼平臺賬號和密碼變量
import requestsdef base64_api(img):# 定義一個函數(shù), 把用戶名,密碼 和圖片路徑傳進去with open(img, 'rb') as f: # 打開請求到的驗證碼圖片base64_data = base64.b64encode(f.read()).decode() # 讀取驗證碼圖片并且轉(zhuǎn)換成字符串類型data = {"username": KUAI_USER , "password": KUAI_PASS, "typeid": 21, "image": base64_data} # 用戶名和密碼導(dǎo)入,typeid是根據(jù)開發(fā)文檔查到的對應(yīng)接口數(shù)字(B站驗證碼為圖片坐標點選類型), image是圖片字符串數(shù)據(jù)result = requests.post("http://api.ttshitu.com/predict", data=data).json() # 攜帶data參數(shù),發(fā)送post請求,返回json數(shù)據(jù)if result['success']:return result["data"]["result"]else:return result["message"]if __name__ == "__main__":img_path = "yzm.png" # 相對路徑,由【bili.py】保存在項目文件下result = base64_api(img=img_path) # 把img_path傳遞進去,調(diào)用base64_api函數(shù)print(result)
- 進入谷歌瀏覽器,搜索欄輸入:chrome://version 查看瀏覽器版本
- 進入(https://registry.npmmirror.com/binary.html?path=chromedriver/)查找谷歌瀏覽器對應(yīng)的版本號,下載下來并解壓,放到項目路徑下面
import time
from selenium.webdriver.common.by import By
from accounts import BILI_USER, BILI_PASS
from selenium import webdriver
from selenium.webdriver import ActionChains
from img_api import base64_apidriver = webdriver.Chrome() # 實例化瀏覽器驅(qū)動
driver.get('https://passport.bilibili.com/login') # 發(fā)送請求
driver.implicitly_wait(10) # 隱式等待10秒
driver.maximize_window() # 最大化瀏覽器窗口"""找用戶名和密碼框, 輸入數(shù)據(jù)"""
username_input = driver.find_element(By.CSS_SELECTOR, '#login-username') # 使用css語法定位用戶名輸入框
username_input.click() # 點擊賬號輸入框
time.sleep(0.5) # 強制等待0.5秒
username_input.send_keys(BILI_USER) # 傳入賬號
time.sleep(0.5) # 強制等待0.5秒password_input = driver.find_element(By.CSS_SELECTOR, '#login-passwd') # 使用css語法定位密碼輸入框
password_input.click() # 點擊密碼輸入框
time.sleep(0.5)
password_input.send_keys(BILI_PASS) # 傳入密碼
time.sleep(0.5)"""點擊登錄按鈕"""
time.sleep(0.5)
driver.find_element(By.CSS_SELECTOR, '.btn.btn-login').click() # 使用css語法定位登錄按鈕并點擊
time.sleep(2)# 找驗證碼對應(yīng)的標簽
img_label = driver.find_element(By.CSS_SELECTOR, '.geetest_holder.geetest_silver') # 點擊登錄后跳轉(zhuǎn)到驗證碼驗證界面,使用css語法定位到驗證碼標簽對象"""直接根據(jù)標簽元素保存圖片"""
time.sleep(4) # 保存驗證碼前一定要加強制等待
img_label.screenshot('yzm.png') # 截取驗證碼圖片
time.sleep(4)
print('正在保存驗證碼...')"""識別圖驗證碼"""
code_result_list = base64_api('yzm.png') # 調(diào)用【img_api.py】,返回的數(shù)據(jù)是X,Y 坐標值
print('驗證碼識別結(jié)果為:', code_result_list) # 113,79|197,147result_list = code_result_list.split('|') # ['113,79', '197,147'],轉(zhuǎn)換成列表for result in result_list:x = result.split(',')[0] # 根據(jù)列表索引取出 X 坐標值,是str類型y = result.split(',')[1] # 根據(jù)列表索引取出 Y 坐標值,是str類型# ActionChains 鼠標動作鏈對象# move_to_element_with_offset 根據(jù)元素執(zhí)行點擊操作# perform() 執(zhí)行動作鏈ActionChains(driver).move_to_element_with_offset(img_label, int(x), int(y)).click().perform()time.sleep(5)
"""點擊確認"""
driver.find_element(By.CSS_SELECTOR, '.geetest_commit_tip').click()input()
driver.quit()
- 運行【bili.py】
- 跳轉(zhuǎn)到短信驗證碼登錄頁面,證明打碼成功
總結(jié)
以上是生活随笔為你收集整理的使用打码平台登录B站的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。