Yii Framework 中文网 Python 脚本自动签到

2019-12-17 21:51:20 浏览数 (1)

Yii China 上手动签到了455 天,我都不知道是怎么坚持下来的.........

今天终于换为自动签到了,使用 Python 脚本一键签到。

文章内容来自友链 @沈唁志

1、安装环境 目前 我的 Ubuntu 服务器自带 Python 版本 2.7.12,以及 Python 3.5.2 两个版本

代码语言:javascript复制
python -V
Python 2.7.12

python3 -V
Python 3.5.2

2、使用脚本安装和升级 pip

我们使用 Python 3 来进行开发

代码语言:javascript复制
sudo apt install python3-pip
pip3 -V

3、完整代码

编写一个 yiichina.py 的文件即可,可修改 header 头里面的内容

代码语言:javascript复制
# 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': "1QYGWGk0Js9c3Z1FjfACRzrLr7uspcsECiFCr3XQ6mGBc3wPWgJ3qDiJqwO8m2t2VoDK8u3Ko1ZvTiPcE5K4FA==",
    '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]": "laravel_code@163.com",
    "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()

4、安装依赖模块

安装 requests 模块

代码语言:javascript复制
pip3 install requests

5、添加定时任务

linux 通过定时任务执行

代码语言:javascript复制
crontab -e

根据自己的需要设置定时任务

代码语言:javascript复制
# /www/wwwroot/sign.py 表示对应的文件路径
# /tmp/yiichinasign.log 表示日志路径
30 0 * * * python /var/www/yiichina.py  >> /tmp/yiichina.log 2>&1

如果打印出如下内容则可能是已经签到过或其他问题

代码语言:javascript复制
{"status":0,"message":"未知错误"}

status为 1 时则为签到成功

代码语言:javascript复制
{"status":1,"message":"已连续1天"}

另外 Yii China 舰长目前新开一个新站,名为 php 圈子 网站为 https://php.la 。同样有签到功能,嘿嘿

其他内容和上方一直,只有一处细微差别, data 中的 _csrf 改为 _csrf-frontend 即可。另外 www.yiichina.com 改为 php.la

新增一个 py 文件 phpla.py

代码语言:javascript复制
# coding=utf-8
import re
import requests

# 登录地址
url = "https://php.la/login"
# 签到地址
url2 = "https://php.la/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': "php.la",
    'Origin': "https://php.la",
    'Pragma': "no-cache",
    'Referer': "https://php.la/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': "php.la",
    'Origin': "https://php.la",
    'Pragma': "no-cache",
    'Referer': "https://php.la/",
    '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': "H41rvxjDo58ocyQaOGkn1fwtS_T6KCwizJ1LKQ3zFjFZ-RH-caz0zRohbXZ2CFGUlnkNloxebXH47AZAard4aw==",
    '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-frontend": csrf,
    "LoginForm[username]": "laravel_code@163.com",
    "LoginForm[password]": "*******",
    'LoginForm[rememberMe]': "0",
    "LoginForm[rememberMe]": "1",
    }
    # 完成用户登录
    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-frontend": csrf
    }
    # 模拟签到请求
    response2 = r_session.post(url2, data=data2, headers=headers2)
    print(response2.content)

if __name__ == "__main__":
    login_sign()

以上就是自动签到了

0 人点赞