前言
如题,最近闲着没事,研究出了模拟客户端签到的脚本。
使用
注册账号,过程略。网址:
https://console.cloud.tencent.com/进入云函数控制台的函数服务:
https://console.cloud.tencent.com/scf/index?rid=1 单击“新建”如图(BDUSS及STOKEN获取见附录,粘贴代码时留下它原来的第一行)私信小编01即可获取大量python学习资料
python代码:
from requests import session from hashlib import md5 from random import random from time import sleep class Tieba(): def __init__(self, BDUSS, STOKEN): self.BDUSS = BDUSS self.STOKEN = STOKEN self.count = [0, 0, 0] # 签到成功, 已经签到, 总 self.session = session() self.session.headers.update( {Accept: text/html, */*; q=0.01, Accept-Encoding: gzip, deflate, Accept-Language: zh-CN,zh;q=0.9,en;q=0.8, Connection: keep-alive, Host: tieba.baidu.com, Referer: http://tieba.baidu.com/i/i/forum, User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36, X-Requested-With: XMLHttpRequest} ) def set_cookie(self): self.session.cookies.update({BDUSS: self.BDUSS, STOKEN: self.STOKEN}) def fetch_tbs(self): r = self.session.get(http://tieba.baidu.com/dc/common/tbs).json() if r[is_login] == 1: self.tbs = r[tbs] else: raise Exception(获取tbs错误!以下为返回数据: + str(r)) def fetch_likes(self): self.rest = set() self.already = set() r = self.session.get(https://tieba.baidu.com/mo/q/newmoindex?).json() if r[no] == 0: for forum in r[data][like_forum]: self.count[2] += 1 if forum[is_sign] == 1: self.already.add(forum[forum_name]) else: self.rest.add(forum[forum_name]) else: raise Exception(获取关注贴吧错误!以下为返回数据: + str(r)) def sign(self, forum_name): data = { kw: forum_name, tbs: self.tbs, sign: md5(fkw={forum_name}tbs={self.tbs}tiebaclient!!!.encode(utf8)).hexdigest() } r = self.session.post(http://c.tieba.baidu.com/c/c/forum/sign, data).json() if r[error_code] == 160002: print(f”{forum_name}“已签到!) self.count[1] += 1 return True elif r[error_code] == 0: print(f”{forum_name}“签到成功,您是第{r[“user_info”][“user_sign_rank”]}个签到的用户!) # Modify! self.count[0] += 1 return True else: print(f”{forum_name}“签到失败!以下为返回数据:{str(r)}) return False def loop(self, n): print(f* 开始第{n}轮签到 *) rest = set() self.fetch_tbs() for forum_name in self.rest: sleep(random() * 3) flag = self.sign(forum_name) if not flag: rest.add(forum_name) self.rest = rest def main(self, max): self.set_cookie() self.fetch_likes() n = 0 if self.already: print(* 已经签到的贴吧 *) for forum_name in self.already: print(f”{forum_name}“已签到!) self.count[1] += 1 while n < max and self.rest: n += 1 self.loop(n) print(* 本日签到报告 *) print(f共{self.count[2]}个贴吧,其中签到成功{self.count[0]}个,已经签到{self.count[1]}个,签到失败{len(self.rest)}个。) if self.rest: print(* 签到失败列表 *) for forum_name in self.rest: print(f”{forum_name}“签到失败!) def main_handler(*args): with open(BDUSS.txt) as f: BDUSS = f.read() with open(STOKEN.txt) as f: STOKEN = f.read() task = Tieba(BDUSS, STOKEN) task.main(3) # 不需要推送的删除以下几行 if task.rest: message = f以下为未完成列表: for forum in task.rest: message += \n + forum data = {text: 签到未完全完成!, desp: message} post(https://sc.ftqq.com/你的key.send, data=data)“`如图:
回到此界面,点击”测试”(应该会自动部署,如提示请先部署就先部署一下)
如若成功,应该能看到“测试成功”,输出签到的详细信息,可以进入下一步;如若失败,查看是否复制粘贴出错,无误仍出错请参照附录。设置自动执行
表达式: 0 0 1 * * * *附录/Q&A
BDUSS及STOKEN的获取(以Edge浏览器为例)登录任一百度产品,例如百度一下如图打开cookie界面在”baidu.com/Cookie”下找到”BDUSS”,复制其内容
在”passport.baidu.com/Cookie”下找到”STOKEN”,复制其内容
[b]注意:粘贴到txt文件中时只能一行,如果出现行号为”2″的空行请删除,否则会出错常见错误超时
a. 是否进行了教程的步骤5?
b. 尝试删除”index.py”的第65行(sleep(random() * 3))和第5行(from time import sleep)
c. 仍然超时则关注贴吧个数过多,可以自行修改代码及增加触发器,分次签到,这里不再深入。获取关注贴吧错误
a. 两个txt文件最后是否有空行
b. 是否复制了正确的项到正确的txt,完成后有没有保存
c. 如果这无法解决问题,那重新登录后再次获取
d. 再不行的话我也没办法{:301_971:}© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END