zavier 2018-07-27 10:09:30 722次浏览 3条评论 5 2 0
1、安装环境

首先看一下服务器是否安装Python

$ python -V
$ Python 2.7.5
2、使用脚本安装和升级pip
$ wget https://bootstrap.pypa.io/get-pip.py
$ python get-pip.py
$ pip -V  #查看pip版本
3、直接上代码
# coding=utf-8
import re
import requests

# 登录地址
url = "https://www.yiichina.com/login"
# 签到地址
url2 = "https://www.yiichina.com/registration"

# 构造登陆的header头
headers = {
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    'Accept-Encoding': "gzip, deflate, br",
    'Accept-Language': "zh-CN,zh;q=0.9,en;q=0.8",
    'Cache-Control': "no-cache",
    'Connection': "keep-alive",
    'Content-Length': "250",
    'Content-Type': "application/x-www-form-urlencoded",
    'Host': "www.yiichina.com",
    'Origin': "https://www.yiichina.com",
    'Pragma': "no-cache",
    'Referer': "https://www.yiichina.com/login",
    'Upgrade-Insecure-Requests': "1",
    'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/67.0.3396.99 Safari/537.36 "
}

# 构造签到的header头
headers2 = {
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    'Accept-Encoding': "gzip, deflate, br",
    'Accept-Language': "zh-CN,zh;q=0.9,en;q=0.8",
    'Cache-Control': "no-cache",
    'Connection': "keep-alive",
    'Content-Length': "250",
    'Content-Type': "application/x-www-form-urlencoded",
    'Host': "www.yiichina.com",
    'Origin': "https://www.yiichina.com",
    'Pragma': "no-cache",
    'Referer': "https://www.yiichina.com/",
    'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/67.0.3396.99 Safari/537.36",
    'X-CSRF-Token': "jiZGgvngdp0HiiclI4SheJlJr5-lVyymIFgbfE1ZUw33FWv2mI5DzyrJFUFHt8tO_h3az84GH9xEHEtFCh8cfg==",
    'X-Requested-With': "XMLHttpRequest"
}


# 登陆方法和签到
def login_sign():
    r_session = requests.Session()
    page = r_session.get(url)
    reg = r'<meta name="csrf-token" content="(.+)">'
    csrf = re.findall(reg, page.content)[0]
    data = {
        "_csrf": csrf,
        "LoginForm[username]": "******",
        "LoginForm[password]": "******",
        'LoginForm[rememberMe]': "0",
        "LoginForm[rememberMe]": "1",
        "login - button": ""
    }
    # 完成用户登录
    response1 = r_session.post(url, data=data, headers=headers)
    print(response1.content) #这里可以看到是否模拟登陆成功

    page2 = r_session.get(url2)
    reg = r'<meta name="csrf-token" content="(.+)">'
    csrf = re.findall(reg, page2.content)[0]
    data2 = {
        "_csrf": csrf
    }
    # 模拟签到请求
    response2 = r_session.post(url2, data=data2, headers=headers2)
    print(response2.content)


if __name__ == "__main__":
    login_sign()

header头里面的内容可以直接从浏览器复制过来

4、安装requests模块
$ pip install requests
5、在linux通过定时任务执行
$ crontab -e

更具自己的需要设置定时任务

# 自动签到
30 11 * * * python /root/sign.py # /root/sign.py表示对应的文件路径

这里注意要给文件增加一个写的权限

$ chmod +x sign.py
6、纯粹个人简介,有问题请留言
觉得很赞
您需要登录后才可以评论。登录 | 立即注册