Python签到脚本
checkIn.py脚本内容
import requests
import datetime
import argparse
# 这里的描述会出现在 usage下方 表明这个程序的作用是什么
parser = argparse.ArgumentParser(
description='自动签到脚本')
parser.add_argument("--email", type=str, default="***")
parser.add_argument("--password", type=str, default="***")
parser.add_argument("--domain", type=str, default="https://www.***.lol")
args = parser.parse_args()
# 请求url常量
login_url = "/auth/login"
checkin_url = "/user/checkin"
# 入参 域名账号密码
domain = args.domain
email = args.email
password = args.password
timestamp = int(datetime.datetime.now().timestamp())
print("当前时间戳:", timestamp)
expire_in = timestamp+86400
print("过期时间戳:", expire_in)
# 签到接口
def checkIn(cookie):
# 请求头
headers = {"content-type":"application/x-www-form-urlencoded", "Cookie": cookie}
ret = requests.post(url=domain+checkin_url, headers=headers, timeout=10)
print(ret.json())
# 登陆接口
def login(email, password):
data = {"email": email, "passwd": password}
# 请求头
cookie = "ip=c2c8beac194a8d769d43e802787e9e0e; expire_in=" + str(expire_in)
headers = {"content-type": "application/x-www-form-urlencoded", "Cookie": cookie}
ret = requests.post(url=domain+login_url, data=data, headers=headers, timeout=10)
print(ret.content.decode("unicode-escape"))
return ret.cookies.items();
item = login(email, password)
checkInCookie = ""
for k, v in item:
checkInCookie = checkInCookie+(k + "=" + v)+"; "
if k == "expire_in":
# 过期时间时间戳转格式化并输出
d = datetime.datetime.fromtimestamp(int(v))
d = d + datetime.timedelta(hours=8)
str1 = d.strftime("%Y-%m-%d %H:%M:%S.%f")
print("过期时间:"+str1)
checkInCookie = checkInCookie[:-2]
print(checkInCookie)
checkIn(checkInCookie)
Python环境直接运行
@xunfeng224 ➜ /workspaces/codespaces-jupyter (main) $ /home/codespace/.python/current/bin/python3 /workspaces/codespaces-jupyter/notebooks/checkIn.py
当前时间戳: 1700711196
过期时间戳: 1700797596
{"ret":1,"msg":"登录成功"}
过期时间:2023-11-24 11:46:36.000000
email=xf88023%40163.com; expire_in=1700797596; ip=3c824fe5af499b584d121aa6471bab6e; key=2c043c15c1e4346f90b6f728adfddb426f328e5c03f7b; uid=326769
{'ret': 0, 'msg': '您似乎已经签到过了...'}
pyinstaller打包可执行文件(失败)
以下代码在github CodeSpaces 默认jupyterlab环境执行
要使用 pyinstaller 打包 Python 文件,首先你需要安装 pyinstaller。在命令行中输入以下内容来安装:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
若libpython3.10.so.1.0相关报错,解决方法一:
@xunfeng224 ➜ /workspaces/codespaces-jupyter (main) $ sudo -i
root ➜ /workspaces/codespaces-jupyter (main) $ find / -name libpython3.10.so.1.0
/opt/conda/lib/libpython3.10.so.1.0
find / -name libpython3.10.so.1.0
@xunfeng224 ➜ /workspaces/codespaces-jupyter (main) $ cp /opt/conda/envs/nlp/lib/libpython3.7m.so.1.0 /usr/lib
然后,你可以使用以下命令来打包你的 Python 文件(以 checkIn.py 为例):
pyinstaller checkIn.py -F
这将在你的项目目录下的 dist 文件夹中创建一个名为 checkIn 的可执行文件。
结果该可执行文件在windows环境下无法执行,ubuntu18.04报错
[root@VM-12-13-centos home]# ls
checkIn
[root@VM-12-13-centos home]# chmod 777 checkIn
[root@VM-12-13-centos home]# ./checkIn
Traceback (most recent call last):
File "checkIn.py", line 3, in <module>
ModuleNotFoundError: No module named 'requests'
[369311] Failed to execute script 'checkIn' due to unhandled exception!
Windows计划任务开机执行
Windows上添加计划任务,步骤操作:
- 打开“控制面板”。
- 点击“系统和安全”。
- 点击“管理工具”。
- 找到并点击“任务计划程序”。
- 在“任务计划程序”窗口中,点击右侧的“创建基本任务”。
- 在打开的向导中,输入任务的名称和描述,然后点击“下一步”。
- 选择任务的触发器(例如,开机启动,或者每天的特定时间),然后点击“下一步”。
- 选择任务的操作(例如,启动程序),然后点击“下一步”。
- 确认任务的设置,然后点击“完成”。
设置为开机启动,执行程序为Python脚本checkIn.py