The outputs:
runfile('C:/Users/孙长启/Documents/GitHub/DGUT-yqfk/untitled0.py', wdir='C:/Users/孙长启/Documents/GitHub/DGUT-yqfk')
[SUCCESS] 登陆验证成功
The codes:
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 1 12:09:31 2021
@author: 孙长启
"""
import re
import requests
import time
import sys
import json
import sys
target = []
message = []
def console_msg(msg, level=2):
header = ('[SUCCESS]', '[ERROR]', '[INFO]')
color = ("\033[32;1m", "\033[31;1m", "\033[36;1m")
print(color[level], header[level], msg)
username = "" #input your own user name
password = "" #input your private password
sckey = "SCT80766TY6p3ukoFMlkWGLaJOk5vvqmj"
url = "https://cas.dgut.edu.cn/home/Oauth/getToken/appid/illnessProtectionHome/state/home.html"
session = requests.Session()
origin = session.get(url=url)
html = origin.content.decode('utf-8')
pattern = re.compile(r"var token = \"(.*?)\";", re.MULTILINE | re.DOTALL)
token_tmp = pattern.search(html).group(1) #目的是为了获得token
cookies = {"languageIndex": "0", "last_oauth_appid": "illnessProtectionHome", "last_oauth_state": "home"}
data = {'username': username, 'password': password, '__token__': token_tmp, 'wechat_verif': ''}
headers = {'X-Requested-With': 'XMLHttpRequest'}
response = session.post(url=url, headers=headers, cookies=cookies, data=data).json() #字符串格式
response_json = json.loads(response) #将json格式的数据转换为python中的字典格
if response_json['message'] != '验证通过':
console_msg("登陆验证失败", 1)
message.append(response_json['message'])
else:
console_msg("登陆验证成功", 0)
target.append(response_json['info'])
session.close()
|