前言
内蒙古大学校园网系深澜软件的网页认证系统,每个账号最多同时有3台设备上网,由于种种原因认证网页时常无法打开,因此想到用脚本来模拟人工验证。
实现
python具有强大的第三方库既然要获取网页信息,必然选用requests这一强大的库,通过google chrome的F12中的network获取验证时向服务器发送的内容,配合requests轻松写出脚本。
代码
import requests
import time
def web_sent(user, pwd):
# 校园网登入链接
url_go = "http://172.31.99.50:804/srun_portal_pc.php?ac_id=2&"
url_succeed = "http://172.31.99.50:804/srun_portal_pc_succeed.php"
# 发送的数据
user_data = {
"action": "login",
"username": user,
"password": pwd,
"ac_id": "2",
# "save_me": "1",
"ajax": "1"
}
# 向目标网站发送请求
read = 0
web_return = requests.post(url_go, data=user_data)
while web_return.text == "Portal not response.":
print(web_return.text, "\n网页无响应正在重新登入...")
time.sleep(1) # 每隔1s请求一次,避免服务器拉黑。
# web_return = requests.post(url_go, data=user_data)
read += 1
if read >= 7:
print("服务器忙碌,请重启软件再试一次!!!")
input("Press any key to continue . . .")
return 11
# 输出网页反馈结果
if web_return.status_code == 200:
if web_return.text[0:8] == "login_ok":
print("登录成功!!!")
# 获取ip和账户余额
succeed = requests.post(url_succeed).text
IP = succeed[succeed.rfind("IP地址:") + 76:succeed.rfind("已用流量:") - 59]
Balance = succeed[succeed.rfind("帐户余额:") + 80:succeed.rfind("</span>")]
while IP == '':
succeed = requests.post(url_succeed).text
IP = succeed[succeed.rfind("IP地址:") + 76:succeed.rfind("已用流量:") - 59]
Balance = succeed[succeed.rfind("帐户余额:") + 80:succeed.rfind("</span>")]
print("获取的IP为:", IP)
print("账户余额:", Balance, "¥")
input("Press any key to continue . . .")
else:
print(web_return.text[:web_return.text.rfind(".") + 1])
input("Press any key to continue . . .")
def main():
time.sleep(1)
user = "0000000000" # 此处为校园网账号
pwd = "000000" # 此处为校园网密码
web_sent(user, pwd)
# 执行指令
main()