1.购买服务器阿里云:服务器购买地址https://t.aliyun.com/U/Bg6shY若失效,可用地址
阿里云:
服务器购买地址
https://t.aliyun.com/U/Bg6shY若失效,可用地址
https://www.aliyun.com/daily-act/ecs/activity_selection?source=5176.29345612&userCode=49hts92d腾讯云:
https://curl.qcloud.com/wJpWmSfU若失效,可用地址
https://cloud.tencent.com/act/cps/redirect?redirect=2446&cps_key=ad201ee2ef3b771157f72ee5464b1fea&from=console华为云
https://activity.huaweicloud.com/cps.html?fromacct=64b5cf7cc11b4840bb4ed2ea0b2f4468&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=2019052.部署教程
3.代码如下
# cron: 11 8 * * *import requestsimport re,os,sysimport timefrom notify import senddef pr(message):msg.append(message + "\n" )print(message)msg = []def index(cookie): #验证登录url = 'https://www.hifiti.com/index.htm'header = {"authority":"www.hifiti.com","method":"GET","path":"/index.htm","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36","content-type": "text/html; charset=utf-8; Cache-control:private","referer":"https://hifiti.com/user-login.htm","cookie":cookie}try:response = requests.get(url=url,headers=header)info = response.textif "首页" in info:pr("登陆成功")sing(cookie)else:pr("登录失败")except Exception as e:pr(e)def sing(cookie):#验证是否签到url = 'https://www.hifiti.com/sg_sign.htm'header = {"authority":"www.hifiti.com","method":"GET","path":"/sg_sign.htm","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36","content-type": "text/html; charset=utf-8; Cache-control:private","referer":"https://www.hifiti.com/","cookie":cookie}try:response = requests.get(url=url,headers=header)info = response.textif "已签" in info:pr("您今天已经签到过了,请勿重复签到。")my(cookie)elif "签到" in info:pattern = re.compile(r'var sign = "(.*?)"')matches = pattern.findall(info)if not matches:pr("解析用户信息失败,可能页面结构变化或 cookie 无效")returnsgin = matches[0]sing1(cookie,sgin)time.sleep(3)my(cookie)else :pr("签到失败")except Exception as e:pr(e)def sing1(cookie,sgin):#签到url = 'https://www.hifiti.com/sg_sign.htm'header = {"authority":"www.hifiti.com","method":"POST","path":"/sg_sign.htm","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36","referer":"https://www.hifiti.com/sg_sign.htm","cookie":cookie}data = {"sign":sgin,}try:response = requests.post(url=url,headers=header,data=data)s1 = response.status_codeif s1 == 200 :pr("签到成功")else:pr("失败")except Exception as e:pr(e)def my(cookie):#查询信息url = 'https://www.hifiti.com/my.htm'header = {"authority":"www.hifiti.com","method":"GET","path":"/my.htm","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36","referer":"https://www.hifiti.com/my-post.htm","cookie":cookie}try:response = requests.get(url=url,headers=header)info = response.textif "基本资料" in info:pattern = re.compile(r'"view/img/avatar.png"> (.*?)</a></li>')pattern2 = re.compile(r'金币:</span><b class="text-danger">(.*?)</b>')matches = pattern.findall(info)matches1 = pattern2.findall(info)if not matches or not matches1:pr("解析用户信息失败,可能页面结构变化或 cookie 无效")returnpr( "用户名:" + matches[0] + " 金币" + matches1[0])else:pr("获取错误。")except Exception as e:pr(e)def sicxs():config_path = 'config.py'if os.path.exists(config_path):import configelse:with open(config_path, 'w') as f:pr("首次运行,已创建配置文件 config.py,请按照说明填写相关变量后再次运行脚本。")f.write('#可以在此文件中添加配置变量,例如:\nsfsy = ""\n')try:env_cookie = os.environ.get("wy_hifiti")si_cookie = getattr(config, 'wy_hifiti', '')if env_cookie and si_cookie:cookies = env_cookie + "\n" + si_cookieelif env_cookie:cookies = env_cookieelif si_cookie:cookies = si_cookieelse:pr("请设置变量 export wy_hifiti='' 或在 config.py 中设置 wy_hifiti")sys.exit()except Exception as e:pr("请设置变量 export wy_hifiti='' 或在 config.py 中设置 wy_hifiti")sys.exit()list_cookie = re.split(r'\n|&|@', cookies)total_cookies = len(list_cookie)for i, list_cookie_i in enumerate(list_cookie):print(f'\n----------- 账号【{i + 1}/{total_cookies}】执行 -----------')pr(f"账号【{i + 1}】开始执行:")try:index(list_cookie_i)except Exception as e:pr(f"执行账号【{i + 1}】时发生错误: {e}")finally:send("音乐磁场", ''.join(msg))msg.clear()print(f'\n----------- 执 行 结 束 -----------')if __name__ == '__main__':sicxs()
解析
该脚本为音乐磁场(hifiti.com)自动签到脚本。
主要作用
信息查询脚本:利用传入的
cookie登录站点,判断当日是否已签到;未签则提取签名参数并提交签到;最后进入个人中心页面抓取并输出用户名、金币等信息。多账号与通知:从环境变量或
config.py读取多个账号(@、&、换行分隔),逐个执行并通过notify.send("音乐磁场", …)推送结果;支持定时任务(cron: 11 8 * * *)。
主要方法
pr(message): 统一打印日志并追加到全局msg,方便最终拼装通知文本。index(cookie): 访问https://www.hifiti.com/index.htm验证登录成功与否;成功则进入sing(cookie)。sing(cookie): 访问签到页sg_sign.htm判断状态:包含"已签"→ 已签到,转到
my(cookie)查询资料;包含"签到"→ 用正则提取
var sign = "...",调用sing1(cookie, sgin)提交签到,稍等后再查my(cookie);否则认为签到失败并记录日志。
sing1(cookie, sgin): 向sg_sign.htm发送POST(表单字段sign),HTTP 200 视为签到成功。my(cookie): 访问my.htm,用正则提取用户名与金币并输出;匹配失败提示"页面改版或 cookie 失效"。sicxs()(主入口/调度):初始化/创建
config.py;合并读取
wy_hifiti(环境变量与config.py中同名变量);按
\n|&|@切分多账号,循环index();每个账号执行完发送一次通知并清空
msg。
注意:
本文部分变量已做脱敏处理,仅用于测试和学习研究,禁止用于商业用途,不能保证其合法性,准确性,完整性和有效性,请根据情况自行判断。技术层面需要提供帮助,可以通过打赏的方式进行探讨。
没有评论:
发表评论