当前位置:   article > 正文

利用python+crontab定时任务,实现ikuuu账户(vpn)自动签到

ikuuu

一、思路

1、创建python脚本,调用对应https接口,实现登录、签到功能;
2、创建crontab定时任务,定时执行python签到脚本。

二、实现

1、python脚本

创建一个脚本ikuuuCheckIn.py,内容为:

#!/usr/bin/python
#coding=UTF-8
__author__ = 'huangsan'
import requests
import datetime
#修改默认encoding方式,解决Python中的UnicodeEncodeError编码错误问题
import sys
reload (sys)
sys.setdefaultencoding('utf-8')

def main():
    #打印当前时间
    print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    # 需要签到的账号
    accountList = ['userName@163.com&userPwd']
    print("共需要签到" + str(len(accountList)) + "个账号")
    i = 1
    for account in accountList:
        print("=====正在执行第" + str(i) + "个账号=====")
        email = account.split('&')[0]
        passwd = account.split('&')[1]
        sign_in(email, passwd)
        print("=====第" + str(i) + "个账号,执行完毕=====")
        i += 1
def sign_in(email, passwd):
    # 请求头
    headers = {'Accept':'application/json, text/javascript, */*; q=0.01','Content-Type':'application/x-www-form-urlencoded; charset=UTF-8','user-agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
    # 请求参数
    body = {"email" : email,"passwd" : passwd,}
    # session
    httpSession = requests.session()
    try:
        # 登录
        print("登录")
        loginResp = httpSession.post('https://ikuuu.art/auth/login', headers=headers, data=body)
        print("loginResp=" + loginResp.text.decode("unicode_escape"))
        if 1 != loginResp.json()['ret'] :
            print("登录失败,请检查帐号配置是否错误:email=" + email + ", passwd=" + passwd)
            return
    except Exception as e:
        # 登录异常,记录信息
        raise Exception("登录异常:email=" + str(email) + ", passwd=" + passwd + ", e:" + repr(e))
    print("登录成功")
    try:
        # 签到
        print("签到")
        checkinResp = httpSession.post('https://ikuuu.art/user/checkin')
        print("checkinResp=" + checkinResp.text.decode("unicode_escape"))
        if 1 != checkinResp.json()['ret'] :
            print("签到失败:email=" + email + ", passwd=" + passwd)
            return
    except Exception as e:
        # 签到异常,记录信息
        print("签到异常:email=" + email + ", passwd=" + passwd + ", e:" + repr(e))
    print("签到成功")
# 入口
if __name__ == '__main__':
    main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

2、crontab任务

以Linux为例:
通过crontab -l可查看当前所有定时任务。
通过crontab -e可添加新的定时任务:

30 0 * * * /root/temp/pyTest/ikuuuCheckIn.py >> /root/temp/pyTest/ikuuuCheckIn.log_u_$(date -u +"\%Y\%m").log 2>&1
  • 1

每天0点30分执行一次,执行输出日志追加至对应文件。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/174393?site
推荐阅读
相关标签
  

闽ICP备14008679号