觉得微博手动点赞太过麻烦?
其实自动点赞的实现并不困难!
本篇会有Cookie、session和token方面的知识,不太了解的可以先看下
web前端cookie session及token会话机制详解
我们先通过前两个小节大概了解一下我们Python登录微博的原理,然后第三小节就会跟大家介绍微博自动点赞的代码。
一、实现登陆微博功能
首先进入微博页面后按F12打开开发者工具,将如图的按钮点击后,在浏览器中手动登陆一次,在Network 标签的XHR类型中找到Login请求标签,在Form data下我们可以看到username(用户名)和password(密码),并知道了请求方式是POST,请求的参数有很多我们直接照搬就是。
网页截图
这时,可能学过一些爬虫的同学便会直接上手写出如上雷同的代码,但发现出不来结果
会报错的代码
import requests 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" } 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)
这是因为有些网站并不只是按照"user-agent"判断用户的正常访问的。那我们还需点开Request Headers(请求头)检查可能还有什么字段会用来判断用户正常访问
一般 referer(请求来源页面)、origin(谁发起的请求)、host(主机名及端口号) 字段也常被用于反爬虫,当我们的爬虫无法正常获取数据时,我们可以将请求头里的这些字段照搬进去试试。经过验证,微博网页是以referer 来判断是否是用户正常访问。最后我们还可以把Cookie放进代码里,这样就不用在代码里输入账号密码了。
要注意Cookie并不是永久有效的,若发现自动登录失败,可以重新上网页把新的Cookie复制下来更换
完整的代码如下
import requests 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" } 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标签会出现什么新的内容吧
网页截图
当微博界面点击发送之后,Network标签就会出现update的请求,点进去可以看到,请求地址是https://m.weibo.cn/api/statuses/update,请求方法是 POST。参数有两个一个是content 也就是发送的微博内容,另一个是st,这里的st通过几次的检验,猜测应该是网站的反爬虫措施。这里获得st的方法是通过同为Network标签下的config请求,里面存放了st值,我们将 JSON 格式的字符串转换为字典,然后取到 st 的值
方法
config_req = session.get("https://m.weibo.cn/api/config") config = config_req.json() st = config["data"]["st"] print(st) #每隔一段时间st值会改变
登录和发送微博的完整代码如
import requests 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" } 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": 省略部分内容...}
调整结构后的代码(功能一样)如
import requests class 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 st def 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("请输入发送的内容:"))
三、实现微博自动点赞
完整的代码
import requests class 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 st def 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()
谢谢大家,Python的分享就到此为止,以后如果有好玩的Python程序,我还会继续向大家分享的,希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/huisoul/article/details/116008126