Python简单实现微博自动点赞
发布于 2022-08-18 09:54:07 1.9K0 举报 文章被收录于专栏:毛利学Python 觉得微博手动点赞太过麻烦?其实自动点赞的实现并不困难! 本篇会有Cookie、session和token方面的知识,不太了解的可以先看下 我们先通过前两个小节大概了解一下我们Python登录微博的原理,然后第三小节就会跟大家介绍微博自动点赞的代码。目录
一、实现登陆微博功能
首先进入后按F12打开开发者工具,将如图的按钮点击后,在浏览器中手动登陆一次,在Network 标签的XHR类型中找到Login请求标签,在Form data下我们可以看到username(用户名)和password(密码),并知道了请求方式是POST,请求的参数有很多我们直接照搬就是。「网页截图↓」

「会报错的代码↓」
代码语言:javascript
复制
这是因为有些网站并不只是按照’user-agent’判断用户的正常访问的。那我们还需点开Request Headers(请求头)检查可能还有什么字段会用来判断用户正常访问
import requestsheaders = {<!-- -->'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'}login_data = {<!-- -->'username': '你的用户名','password': '你的密码','savestate': '1','r': 'https://m.weibo.cn/?jumpfrom=weibocom','ec': '0','pagerefer': 'https://m.weibo.cn/login?backURL=https%253A%252F%252Fm.weibo.cn%252F%253Fjumpfrom%253Dweibocom','entry': 'mweibo','wentry': '','loginfrom': '','client_id': '','code': '','qq': '','mainpageflag': '1','hff': '','hfp': '',}login_req = requests.post('https://passport.weibo.cn/sso/login', data=login_data, headers=headers)print(login_req.status_code)

「完整的代码如下↓」
代码语言:javascript
复制
import requestsheaders = {<!-- -->'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36','referer': 'https://passport.weibo.cn/signin/login? entry=mweibo&res=wel&wm=3349&r=https%3A%2F%2Fm.weibo.cn%2F%3Fjumpfrom%3Dweibocom','cookie': '你的cookie'}login_data = {<!-- -->'savestate': '1','r': 'https://m.weibo.cn/?jumpfrom=weibocom','ec': '0','pagerefer': 'https://m.weibo.cn/login?backURL=https%253A%252F%252Fm.weibo.cn%252F%253Fjumpfrom%253Dweibocom','entry': 'mweibo','wentry': '','loginfrom': '','client_id': '','code': '','qq': '','mainpageflag': '1','hff': '','hfp': '',}login_req = requests.post('https://passport.weibo.cn/sso/login', data=login_data, headers=headers)print(login_req.status_code) #输出200则代表登录成功
二、实现发送微博
既然都登陆微博了,我们先试试能不能顺便发微博吧 同样的,在微博编辑页面点击F12进入开发者工具,我们先试试发送一个微博,Network标签会出现什么新的内容吧「网页截图↓」


「方法↓」
代码语言:javascript
复制
config_req = session.get('https://m.weibo.cn/api/config')config = config_req.json()st = config['data']['st']print(st) #每隔一段时间st值会改变
「登录和发送微博的完整代码如↓」
代码语言:javascript
复制
import requestsheaders = {<!-- -->'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36','referer': 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=https%3A%2F%2Fm.weibo.cn%2F%3Fjumpfrom%3Dweibocom','cookie': '你的cookie'}login_data = {<!-- -->'savestate': '1','r': 'https://m.weibo.cn/?jumpfrom=weibocom','ec': '0','pagerefer': 'https://m.weibo.cn/login?backURL=https%253A%252F%252Fm.weibo.cn%252F%253Fjumpfrom%253Dweibocom','entry': 'mweibo','wentry': '','loginfrom': '','client_id': '','code': '','qq': '','mainpageflag': '1','hff': '','hfp': '',}# 使用 session来保留登录状态session = requests.Session()session.headers.update(headers)login_req = session.post('https://passport.weibo.cn/sso/login', data=login_data)# 获取 st 请求config_req = session.get('https://m.weibo.cn/api/config')config = config_req.json()st = config['data']['st']compose_data = {<!-- -->'content': input("请输入发送的内容:"),,'st': st}compose_req = session.post('https://m.weibo.cn/api/statuses/update', data=compose_data)print(compose_req.json()) # 输出:{<!-- -->'ok': 1, 'data': 省略部分内容...}
「调整结构后的代码(功能一样)如↓」
代码语言:javascript
复制
import requestsclass WeiboSpider:def __init__(self):self.session = requests.Session()self.headers = {<!-- -->'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36','referer': 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=https%3A%2F%2Fm.weibo.cn%2F%3Fjumpfrom%3Dweibocom','cookie': '你的Cookie'}self.session.headers.update(self.headers)def login(self):login_data = {<!-- -->'savestate': '1','r': 'https://m.weibo.cn/?jumpfrom=weibocom','ec': '0','pagerefer': 'https://m.weibo.cn/login?backURL=https%253A%252F%252Fm.weibo.cn%252F%253Fjumpfrom%253Dweibocom','entry': 'mweibo','wentry': '','loginfrom': '','client_id': '','code': '','qq': '','mainpageflag': '1','hff': '','hfp': '',}self.session.post('https://passport.weibo.cn/sso/login', data=login_data)def get_st(self):config_req = self.session.get('https://m.weibo.cn/api/config')config = config_req.json()st = config['data']['st']return stdef compose(self, content):compose_data = {<!-- -->'content': content,'st': self.get_st()}compose_req = self.session.post('https://m.weibo.cn/api/statuses/update', data=compose_data)print(compose_req.json())def send(self, content):self.login()self.compose(content)weibo = WeiboSpider()weibo.send(input("请输入发送的内容:"))
三、实现微博自动点赞
完整的代码↓代码语言:javascript
复制
谢谢大家,Python的分享就到此为止,以后如果有好玩的Python程序,我还会继续向大家分享的
import requestsclass WeiboSpider:def __init__(self):self.session = requests.Session()self.headers = {<!-- -->'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36','referer': 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=https%3A%2F%2Fm.weibo.cn%2F%3Fjumpfrom%3Dweibocom','cookie': '你的cookie'}self.session.headers.update(self.headers)def login(self):login_data = {<!-- -->'savestate': '1','r': 'https://m.weibo.cn/?jumpfrom=weibocom','ec': '0','pagerefer': 'https://m.weibo.cn/login?backURL=https%253A%252F%252Fm.weibo.cn%252F%253Fjumpfrom%253Dweibocom','entry': 'mweibo','wentry': '','loginfrom': '','client_id': '','code': '','qq': '','mainpageflag': '1','hff': '','hfp': '',}self.session.post('https://passport.weibo.cn/sso/login', data=login_data)def get_st(self):config_req = self.session.get('https://m.weibo.cn/api/config')config = config_req.json()st = config['data']['st']return stdef compose(self, content):compose_data = {<!-- -->'content': content,'st': self.get_st()}compose_req = self.session.post('https://m.weibo.cn/api/statuses/update', data=compose_data)print(compose_req.json())def send(self, content):self.login()self.compose(content)# 获取微博列表def get_weibo_list(self):params = {<!-- -->'type': 'uid','value': '2139359753','containerid': '1076032139359753'}weibo_list_req = self.session.get('https://m.weibo.cn/api/container/getIndex', params=params)weibo_list_data = weibo_list_req.json()weibo_list = weibo_list_data['data']['cards']return weibo_list# 点赞微博def vote_up(self, id):vote_up_data = {<!-- -->'id': id, # 要点赞的微博 id'attitude': 'heart','st': self.get_st()}vote_up_req = self.session.post('https://m.weibo.cn/api/attitudes/create', data=vote_up_data)json = vote_up_req.json()print(json['msg'])# 批量点赞微博def vote_up_all(self):self.login()weibo_list = self.get_weibo_list()for i in weibo_list:# card_type 为 9 是正常微博if i['card_type'] == 9:self.vote_up(i['mblog']['id'])weibo = WeiboSpider()weibo.vote_up_all()
