yiiCaina 站点自动签到程序 (python)

yiiCaina 站点自动签到程序 (python)

https://www.yiichina.com/

# coding=utf-8
import requests
import smtplib
import json
from pyquery import PyQuery as pq
from email.mime.text import MIMEText
from email.header import Header

#安装插件
# pip3.7 install requests
# pip3.7 install PyQuery

# 登录地址
login_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(login_url)
    # 请求获得的内容
    content = pq(page.content)

    # 获取csrf_token
    item1 = content('meta[name=csrf-token]').items()
    csrf = ''
    for nurl in item1:
        csrf = nurl.attr('content')

    data = {
        "_csrf": csrf,
        "LoginForm[username]": "**********",
        "LoginForm[password]": "**********",
        'LoginForm[rememberMe]': "0",
        "LoginForm[rememberMe]": "1",
        "login - button": ""
    }
    # 完成用户登录
    response1 = r_session.post(login_url, data=data, headers=headers)
    # 请求获得的内容
    content = pq(response1.content)

    # 获取csrf_token
    item1 = content('meta[name=csrf-token]').items()
    csrf = ''
    for nurl in item1:
        csrf = nurl.attr('content')

    data2 = {
        "_csrf": csrf
    }
    # 模拟签到请求
    response2 = r_session.post(url2, data=data2, headers=headers2)
    data = response2.text
    if data is not '':
        data = json.loads(data, encoding='utf-8')
        if data['status'] == 0:
            print(data['message'])
            sendEmail('yii China 签到程序,结果:' + data['message'])
        else:
            print(data)
            sendEmail('yii China 签到程序,结果:' + data['message'])
    else:
        sendEmail('yii China 签到程序,请求无结果!')

def sendEmail(content: object) -> object:
    receivers = ['879042886@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

    sender = 'send@majiameng.com'
    subject = 'Python 签到警告!' #标题
    # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
    message = MIMEText(content, 'plain', 'utf-8')
    message['From'] = Header(sender, 'utf-8')  # 发送者
    message['To'] = Header(str(receivers), 'utf-8')  # 接收者

    message['Subject'] = Header(subject, 'utf-8')

    try:
        # 一般我们配置的smtp服务器端口都是25,不过有的服务器或空间提供商把25端口给禁用了,
        # 比如阿里云就给禁用了,这个可以找相应的提供商确认一下。
        # 如果真是禁用了25端口,可以采用465端口,这个端口很多主流的邮件服务商像网易邮箱、QQ邮箱、阿里云邮箱也都支持,采用了465端口,
        # smtpObj = smtplib.SMTP('smtp.mxhichina.com')
        smtpObj = smtplib.SMTP_SSL('邮箱网关', 465)
        smtpObj.login('邮箱帐号', '邮箱密码')
        smtpObj.sendmail(sender, receivers, message.as_string())
        smtpObj.close()
        print("Success: 邮件发送成功")
        return True
    except smtplib.SMTPException as e:
        print("Error: 无法发送邮件" + str(e))
        return False


if __name__ == "__main__":
    try:
        login_sign()
    except Exception as e:
        print(str(e))
        sendEmail('yii China 签到程序,异常:' + str(e))
Last modification:July 10th, 2019 at 07:13 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment