本篇內容主要講解“基于Python怎么編寫一個微博抽獎小程序”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“基于Python怎么編寫一個微博抽獎小程序”吧!
Python版本:3.7.8
相關模塊:
DecryptLogin模塊;
DecryptLoginExamples模塊;
以及一些python自帶的模塊。
安裝Python并添加到環境變量,pip安裝需要的相關模塊即可。
首先,pip安裝一下DecryptLoginExamples模塊:
pip install DecryptLoginExamples
然后簡單寫幾行代碼調用就ok啦:
from DecryptLoginExamples import client
config = {
'username': 用戶名,
'password': 密碼,
'time_interval': 查詢微博動態的間隔時間,
}
crawler_executor = client.Client()
crawler_executor.executor('weibolottery', config=config)效果如下:

整個實現流程和之前的這篇文章差不多:
不想內卷了,整個B站全自動抽獎的小程序吧,萬一不小心暴富了呢~
具體而言,就是先獲取自己微博的關注列表:
'''獲得關注的用戶列表'''
def getfollows(self, session):
page, targetid_list = 0, []
while True:
page += 1
response = session.get('https://m.weibo.cn/api/container/getIndex?containerid=231093_-_selffollowed&page={}'.format(page), headers=self.headers)
profile_urls = re.findall(r'"profile_url":"(.*?)"', response.text)
if len(profile_urls) == 0: break
for profile_url in profile_urls:
targetid_list.append(re.findall(r'uid=(.*?)&', profile_url)[0])
return targetid_list然后定時檢測自己關注的用戶有沒有發布新的抽獎信息就ok了:
# 每隔一段時間遍歷一遍目標用戶, 把有抽獎信息的微博都轉發一遍
self.logging('初始化完成, 開始自動檢測抽獎相關的微博')
while True:
for targetid in targetid_list:
print(f'正在檢測用戶{targetid}是否發布了新的抽獎微博')
weibos = self.getweibos(session, targetid)
for card in weibos:
if card['mblog']['id'] in repost_weibos_dict[targetid]:
continue
else:
repost_weibos_dict[targetid].append(card['mblog']['id'])
if '抽獎' in card['mblog']['text']:
self.logging(f'檢測到一條疑似含有抽獎信息的微博: {card}')
# 自動點贊
card_id = card['mblog']['id']
response = session.get('https://m.weibo.cn/api/config')
st = response.json()['data']['st']
flag, response_json = self.starweibo(session, st, card_id, targetid)
if flag:
self.logging(f'自動點贊ID為{card_id}的微博成功')
else:
self.logging(f'自動點贊ID為{card_id}的微博失敗, 返回的內容為 >>>\n{response_json}')
# 自動轉發+評論
flag, response_json = self.repost(session, st, card_id)
if flag:
self.logging(f'自動轉發+評論ID為{card_id}的微博成功')
else:
self.logging(f'自動轉發+評論ID為{card_id}的微博失敗, 返回的內容為 >>>\n{response_json}')
print(f'檢測用戶{targetid}是否發布了新的抽獎微博完成')
time.sleep(self.time_interval)其中,判斷這條微博是否屬于抽獎微博的方式是:
if '抽獎' in card['mblog']['text']:
到此,相信大家對“基于Python怎么編寫一個微博抽獎小程序”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。