基于Python编写一个微博抽奖小程序
作者:白露未晞me 时间:2023-04-02 16:40:00
导语
带大家写个微博自动抽奖小程序吧,motivation和之前的B站自动抽奖小程序一样:
不想内卷了,整个B站全自动抽奖的小程序吧,万一不小心暴富了呢~
废话不多说,让我们愉快地开始吧~
开发工具
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']:
即微博正文中存在抽奖这两个字的时候,我们就对该微博进行点赞,自动转发和评论操作,所以可能存在误转的情况。不过这玩意应该是属于宁滥勿缺吧。
ok,大功告成啦
来源:https://mp.weixin.qq.com/s/sGT4Pwp-yu2grNvSr3vafQ
标签:Python,微博,抽奖,程序


猜你喜欢
pytorch 常用函数 max ,eq说明
2023-06-25 11:09:21

php+mysqli数据库连接的两种方式
2023-10-08 22:15:16
各种页面定时跳转(倒计时跳转)代码总结
2023-09-05 00:12:01
python多线程之事件Event的使用详解
2022-12-21 11:46:10
python小程序基于Jupyter实现天气查询的方法
2021-05-30 10:39:40

教你如何使用Python快速爬取需要的数据
2022-04-14 16:14:50

在Vue中使用echarts的方法
2024-06-07 16:01:42
JavaScript setTimeout与setTimeinterval使用案例详解
2024-04-18 09:45:10
Python快速排序算法实例分析
2021-10-23 09:14:37

Python装饰器有哪些绝妙的用法
2022-09-08 01:09:47
快速了解Python开发环境Spyder
2023-01-29 14:37:47

详解python开发环境搭建
2023-09-17 21:37:25

JavaScript资源预加载组件和滑屏组件的使用推荐
2024-04-22 22:35:11
Python3爬虫里关于代理的设置总结
2021-05-05 23:58:12
C#实现MySQL命令行备份和恢复
2024-01-21 02:19:48
Python 模块EasyGui详细介绍
2022-04-27 22:55:39
Ubuntu配置Pytorch on Graph (PoG)环境过程图解
2022-01-13 02:05:12

Python学习之运算符号
2023-11-29 04:11:04
Python字典循环添加一键多值的用法实例
2021-07-15 14:48:48
php中使用key,value,current,next和prev函数遍历数组的方法
2023-10-18 20:17:39