基于Python編寫一個(gè)B站全自動(dòng)抽獎(jiǎng)的小程序
導(dǎo)語
應(yīng)好友邀請(qǐng),幫他寫了個(gè)小程序,功能類似于實(shí)時(shí)監(jiān)控自己關(guān)注的UP主,如果關(guān)注的UP主中有人發(fā)布了抽獎(jiǎng)的動(dòng)態(tài),就自動(dòng)參與這個(gè)抽獎(jiǎng)。這樣就能不錯(cuò)過任何一個(gè)可以暴富的機(jī)會(huì)了。寫完之后感覺這個(gè)想法還是挺有意思的,于是上來分享一波。
廢話不多說,讓我們愉快地開始吧~
開發(fā)工具
Python版本:3.7.8
相關(guān)模塊:
DecryptLogin模塊;
以及一些python自帶的模塊。
環(huán)境搭建
安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。
原理簡(jiǎn)介
我們主要用到的工具是公眾號(hào)前幾天剛發(fā)布的DecryptLogin包
首先,我們需要用它來模擬登錄B站,具體而言,需要先pip安裝一下:
pip install DecryptLogin
然后利用Client類來實(shí)現(xiàn)模擬登錄,該類可以保存當(dāng)前的登錄會(huì)話,在該會(huì)話沒過期之前再次運(yùn)行程序是不需要重復(fù)發(fā)送登錄請(qǐng)求的,可以避免因?yàn)轭l繁登錄賬號(hào)而觸發(fā)網(wǎng)站的風(fēng)控機(jī)制:
from DecryptLogin import login client = login.Client() bili = client.bilibili(reload_history=True) infos_return, session = bili.login(self.username, '微信公眾號(hào): Charles的皮卡丘', 'scanqr')
接著,我們來抓包看看抓取自己的關(guān)注列表要請(qǐng)求哪個(gè)API吧,具體而言,如下圖所示:

代碼簡(jiǎn)單實(shí)現(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)前所有動(dòng)態(tài)的接口如下:
'''獲得UP主的動(dòng)態(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ā)動(dòng)態(tài)的接口如下:
'''轉(zhuǎn)發(fā)動(dòng)態(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()接下來要做的就是如何判斷這個(gè)動(dòng)態(tài)是不是抽獎(jiǎng)動(dòng)態(tài)了,這里我們?cè)O(shè)置利用關(guān)鍵詞作為判斷依據(jù):
# 監(jiān)控新的動(dòng)態(tài)
self.logging('開始監(jiān)控是否有新的抽獎(jiǎng)信息發(fā)布')
while True:
time.sleep(self.time_interval)
self.logging('開始檢測(cè)是否有新的抽獎(jiǎng)信息發(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 '#互動(dòng)抽取#' in desp:
result = self.forwardupdate(session, infos_return, dynamic_id)
self.logging('檢測(cè)到有新的抽獎(jiǎng)信息發(fā)布, 已經(jīng)嘗試轉(zhuǎn)發(fā), 返回的結(jié)果為{result}')
followings_infos[userid] = updates_latest即當(dāng)動(dòng)態(tài)中有#互動(dòng)抽取#這四個(gè)字的時(shí)候,我們就認(rèn)定這是一條抽獎(jiǎng)用的動(dòng)態(tài),并對(duì)其進(jìn)行轉(zhuǎn)發(fā)。至此,我們的小程序就完成啦,它可以實(shí)時(shí)監(jiān)控我們關(guān)注的UP主是否有發(fā)布新的抽獎(jiǎng)信息,如果有,則第一時(shí)間參與這個(gè)抽獎(jiǎng)。我們需要做的就是多關(guān)注一些經(jīng)常發(fā)布抽獎(jiǎng)信息的UP主就行了,接下來能不能暴富就看運(yùn)氣了。
最終代碼的使用方式如下:
usage: bilibililottery.py [-h] [--username USERNAME]
[--time_interval TIME_INTERVAL]
B站監(jiān)控關(guān)注的UP主并自動(dòng)轉(zhuǎn)發(fā)抽獎(jiǎng)
optional arguments:
-h, --help show this help message and exit
--username USERNAME 用于存儲(chǔ)歷史cookies的唯一標(biāo)識(shí)ID
--time_interval TIME_INTERVAL
查詢UP主的動(dòng)態(tài)的間隔時(shí)間到此這篇關(guān)于基于Python編寫一個(gè)B站全自動(dòng)抽獎(jiǎng)的小程序的文章就介紹到這了,更多相關(guān)Python自動(dòng)抽獎(jiǎng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析python3中的os.path.dirname(__file__)的使用
這篇文章主要介紹了python3中的os.path.dirname(__file__)的使用,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
詳解Python中高階函數(shù)(map,filter,reduce,sorted)的使用
高階函數(shù)就是能夠把函數(shù)當(dāng)成參數(shù)傳遞的函數(shù)就是高階函數(shù),換句話說如果一個(gè)函數(shù)的參數(shù)是函數(shù),那么這個(gè)函數(shù)就是一個(gè)高階函數(shù)。本文為大家詳細(xì)講解了Python中常用的四個(gè)高階函數(shù),感興趣的可以了解一下2022-04-04
Python API 自動(dòng)化實(shí)戰(zhàn)詳解(純代碼)
今天小編就為大家分享一篇Python API 自動(dòng)化實(shí)戰(zhàn)詳解(純代碼),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06
如何測(cè)試Python網(wǎng)站的訪問速度,并且優(yōu)化Python網(wǎng)站的性能
本文使用網(wǎng)絡(luò)工具和Python測(cè)速庫進(jìn)行測(cè)試Python網(wǎng)站的訪問速度,通過優(yōu)化代碼性能和優(yōu)化服務(wù)器性能以及優(yōu)化數(shù)據(jù)庫性能等有針對(duì)性地優(yōu)化Python網(wǎng)站的性能2024-01-01
python實(shí)現(xiàn)從本地?cái)z像頭和網(wǎng)絡(luò)攝像頭截取圖片功能
這篇文章主要介紹了python實(shí)現(xiàn)從本地?cái)z像頭和網(wǎng)絡(luò)攝像頭截取圖片功能 ,文中給大家提到了python , opencv 打開網(wǎng)絡(luò)攝像頭讀取圖像的實(shí)現(xiàn)代碼,需要的朋友可以參考下2019-07-07

