基于Python編寫一個B站全自動抽獎的小程序
導(dǎo)語
應(yīng)好友邀請,幫他寫了個小程序,功能類似于實時監(jiān)控自己關(guān)注的UP主,如果關(guān)注的UP主中有人發(fā)布了抽獎的動態(tài),就自動參與這個抽獎。這樣就能不錯過任何一個可以暴富的機會了。寫完之后感覺這個想法還是挺有意思的,于是上來分享一波。
廢話不多說,讓我們愉快地開始吧~
開發(fā)工具
Python版本:3.7.8
相關(guān)模塊:
DecryptLogin模塊;
以及一些python自帶的模塊。
環(huán)境搭建
安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。
原理簡介
我們主要用到的工具是公眾號前幾天剛發(fā)布的DecryptLogin包
首先,我們需要用它來模擬登錄B站,具體而言,需要先pip安裝一下:
pip install DecryptLogin
然后利用Client類來實現(xiàn)模擬登錄,該類可以保存當(dāng)前的登錄會話,在該會話沒過期之前再次運行程序是不需要重復(fù)發(fā)送登錄請求的,可以避免因為頻繁登錄賬號而觸發(fā)網(wǎng)站的風(fēng)控機制:
from DecryptLogin import login client = login.Client() bili = client.bilibili(reload_history=True) infos_return, session = bili.login(self.username, '微信公眾號: Charles的皮卡丘', 'scanqr')
接著,我們來抓包看看抓取自己的關(guān)注列表要請求哪個API吧,具體而言,如下圖所示:
代碼簡單實現(xiàn)如下:
'''獲得關(guān)注列表''' def getfollowings(self, session, infos_return): url = 'https://api.bilibili.com/x/relation/followings' params = { 'vmid': infos_return['data']['mid'], 'pn': '1', 'ps': '20', 'order': 'desc', 'order_type': 'attention', 'jsonp': 'jsonp', } response = session.get(url, params=params, headers=self.headers) total = response.json()['data']['total'] followings_ids, page = [], 1 while True: for item in response.json()['data']['list']: followings_ids.append(item['mid']) if len(followings_ids) >= total: break page += 1 params['pn'] = str(page) response = session.get(url, params=params, headers=self.headers) return followings_ids
類似地,我們可以獲得我們關(guān)注的UP主的當(dāng)前所有動態(tài)的接口如下:
'''獲得UP主的動態(tài)''' def getupdates(self, infos_return, host_uid, session): url = f'https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history?visitor_uid={infos_return["data"]["mid"]}&host_uid={host_uid}&offset_dynamic_id=0&need_top=1&platform=web' response = session.get(url, headers=self.headers) response_json, updates = response.json(), {} for card in response_json['data']['cards']: dynamic_id = card['desc']['dynamic_id'] desp = re.findall(r'"description":"(.*?)"', card['card'])[0] updates[dynamic_id] = desp return updates
轉(zhuǎn)發(fā)動態(tài)的接口如下:
'''轉(zhuǎn)發(fā)動態(tài)''' def forwardupdate(self, session, infos_return, dynamic_id): url = 'http://api.vc.bilibili.com/dynamic_repost/v1/dynamic_repost/repost' data = { 'uid': infos_return['data']['mid'], 'dynamic_id': dynamic_id, 'content' : random.choice(self.comments), 'ctrl': '[{"data":"5581898","location":2,"length":4,"type":1},{"data":"10462362","location":7,"length":5,"type":1},{"data":"1577804","location":13,"length":4,"type":1}]', 'csrf_token': session.cookies.get('bili_jct') } response = session.post(url, data=data, headers=self.headers) return response.json()
接下來要做的就是如何判斷這個動態(tài)是不是抽獎動態(tài)了,這里我們設(shè)置利用關(guān)鍵詞作為判斷依據(jù):
# 監(jiān)控新的動態(tài) self.logging('開始監(jiān)控是否有新的抽獎信息發(fā)布') while True: time.sleep(self.time_interval) self.logging('開始檢測是否有新的抽獎信息發(fā)布') for userid in tqdm(followings_ids): updates_old = followings_infos.pop(userid) updates_latest = self.getupdates(infos_return, userid, session) for dynamic_id in updates_latest.keys(): if dynamic_id not in updates_old: desp = updates_latest[dynamic_id] if '#互動抽取#' in desp: result = self.forwardupdate(session, infos_return, dynamic_id) self.logging('檢測到有新的抽獎信息發(fā)布, 已經(jīng)嘗試轉(zhuǎn)發(fā), 返回的結(jié)果為{result}') followings_infos[userid] = updates_latest
即當(dāng)動態(tài)中有#互動抽取#這四個字的時候,我們就認定這是一條抽獎用的動態(tài),并對其進行轉(zhuǎn)發(fā)。至此,我們的小程序就完成啦,它可以實時監(jiān)控我們關(guān)注的UP主是否有發(fā)布新的抽獎信息,如果有,則第一時間參與這個抽獎。我們需要做的就是多關(guān)注一些經(jīng)常發(fā)布抽獎信息的UP主就行了,接下來能不能暴富就看運氣了。
最終代碼的使用方式如下:
usage: bilibililottery.py [-h] [--username USERNAME] [--time_interval TIME_INTERVAL] B站監(jiān)控關(guān)注的UP主并自動轉(zhuǎn)發(fā)抽獎 optional arguments: -h, --help show this help message and exit --username USERNAME 用于存儲歷史cookies的唯一標識ID --time_interval TIME_INTERVAL 查詢UP主的動態(tài)的間隔時間
到此這篇關(guān)于基于Python編寫一個B站全自動抽獎的小程序的文章就介紹到這了,更多相關(guān)Python自動抽獎內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析python3中的os.path.dirname(__file__)的使用
這篇文章主要介紹了python3中的os.path.dirname(__file__)的使用,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08詳解Python中高階函數(shù)(map,filter,reduce,sorted)的使用
高階函數(shù)就是能夠把函數(shù)當(dāng)成參數(shù)傳遞的函數(shù)就是高階函數(shù),換句話說如果一個函數(shù)的參數(shù)是函數(shù),那么這個函數(shù)就是一個高階函數(shù)。本文為大家詳細講解了Python中常用的四個高階函數(shù),感興趣的可以了解一下2022-04-04如何測試Python網(wǎng)站的訪問速度,并且優(yōu)化Python網(wǎng)站的性能
本文使用網(wǎng)絡(luò)工具和Python測速庫進行測試Python網(wǎng)站的訪問速度,通過優(yōu)化代碼性能和優(yōu)化服務(wù)器性能以及優(yōu)化數(shù)據(jù)庫性能等有針對性地優(yōu)化Python網(wǎng)站的性能2024-01-01python實現(xiàn)從本地攝像頭和網(wǎng)絡(luò)攝像頭截取圖片功能
這篇文章主要介紹了python實現(xiàn)從本地攝像頭和網(wǎng)絡(luò)攝像頭截取圖片功能 ,文中給大家提到了python , opencv 打開網(wǎng)絡(luò)攝像頭讀取圖像的實現(xiàn)代碼,需要的朋友可以參考下2019-07-07