用python实现的NYOJ自动签到程序

程序简介

  • 本程序能够实现登录NYOJ,并自动签到。签到成功后会提示签到信息,展示用户当前所有的NYOJ币。
  • NYOJ币可以用于查看AC代码,便于学习他人的解题思路,扩展思维。

使用说明

  • 程序基于python3
  • 需要安装requests库和BeautifulSoup4库
  • 这是个初学者作品,理性吐槽哟

import requests

from bs4 import BeautifulSoup

def login_check(response):
    '''抓取获得登录结果'''
    response.encoding = response.apparent_encoding
    soup = BeautifulSoup(response.text, 'html.parser')
    lists = soup.find_all('a')
    if lists[3].string == '登录':
        return False
    else:
        return True

def getPostData():
    '''获得登录信息字典'''
    userid = input("请输入用户名:")
    password = input("请输入密码:")
    post_data = {'userid':userid, 'password':password}
    return post_data

def getMessage(response):
    '''获得签到信息'''
    response.encoding = response.apparent_encoding
    soup = BeautifulSoup(response.text, 'html.parser')
    lists = soup.find_all('a')
    massage = str(lists[3]['href'])
    msp = massage.find("'") + 1
    mep = massage.find("'",msp + 1)
    return massage[msp:mep]

def getNyojCoin(response):
    '''获得Nyoj币的信息'''
    response.encoding = response.apparent_encoding
    soup = BeautifulSoup(response.text, 'html.parser')
    lists = soup.find_all('a')
    return lists[4].string

def getCoinSoup():
    '''获得签到和登录的response对象,以列表形式返回两个对象
        登录失败时返回的是包含提示信息的字符串'''
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36'
        }
        url_qd = 'http://acm.nyist.edu.cn/JudgeOnline/problemset.php?flag=1'
        url = 'http://acm.nyist.edu.cn/JudgeOnline'
        post_data = getPostData()
        post_url = 'http://acm.nyist.edu.cn/JudgeOnline/dologin.php'
        r = requests.session()
        psoup = r.post(post_url, headers=headers, data=post_data)
        if not login_check(psoup):
            return 'Login Failure'
        r.get(url_qd, headers=headers)
        soup = r.get(url, headers=headers)
        return [psoup, soup]
    except:
        return '404'

def getCoinSoup_step(userid, password):
    '''获得签到和登录的response对象,以列表形式返回两个对象
        登录失败时返回的是包含提示信息的字符串'''
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36'
        }
        url_qd = 'http://acm.nyist.edu.cn/JudgeOnline/problemset.php?flag=1'
        url = 'http://acm.nyist.edu.cn/JudgeOnline'
        post_data = {'userid':userid,'password':password}
        post_url = 'http://acm.nyist.edu.cn/JudgeOnline/dologin.php'
        r = requests.session()
        psoup = r.post(post_url, headers=headers, data=post_data)
        if not login_check(psoup):
            return 'Login Failure'
        r.get(url_qd, headers=headers)
        soup = r.get(url, headers=headers)
        return [psoup, soup]
    except:
        return '404'

def get_nyoj_coin():
    '''签到功能实现的主程序'''
    test = []
    lists = getCoinSoup()
    if type(lists) == type(test):
        psoup,soup = lists[0],lists[1]
        print(getMessage(psoup))
        print("目前" + getNyojCoin(soup))
    elif lists == '404':
        print('签到失败!请检查网络后重试\n(如果您手动访问acm.nyoj.edu.cn失败,那么该程序也无法完成签到)')
    elif lists == 'Login Failure':
        print('签到失败!用户名或密码错误')

def get_nyoj_coin_step(userid, password):
    '''签到功能实现的主程序(方便个人使用,不用重复输入)'''
    test = []
    lists = getCoinSoup_step(userid, password)
    if type(lists) == type(test):
        psoup,soup = lists[0],lists[1]
        print(getMessage(psoup))
        print("目前" + getNyojCoin(soup))
    elif lists == '404':
        print('签到失败!请检查网络后重试\n(如果您手动访问acm.nyoj.edu.cn失败,那么该程序也无法完成签到)')
    elif lists == 'Login Failure':
        print('签到失败!用户名或密码错误')

if __name__ == '__main__':
    get_nyoj_coin()

声明

  • 本人作为一个python的初学者,所有程序设计都是为了方便个人使用,程序设计皆参照个人使用习惯,发布出来是方便其他入门学习者学习交流。
  • 任何时候,程序皆在相关规范之内设计和使用,其他个人使用者可以对程序做便于自己使用的修改,但也应遵循相关规范。任何商业用途和非法用途,本人皆不认同,且后果自负。

猜你喜欢

转载自blog.csdn.net/teolih/article/details/81671245