-
Notifications
You must be signed in to change notification settings - Fork 3
/
login.py
26 lines (22 loc) · 909 Bytes
/
login.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from urllib.parse import quote
from bs4 import BeautifulSoup
import requests
login_url = 'https://sso.buaa.edu.cn/login?service='
def get_token(session: requests.Session, target: str) -> str:
response = session.get(target)
soup = BeautifulSoup(response.text, 'html.parser')
token = soup.find('input', {'name': 'execution'})['value']
return token
def login(session: requests.Session, target_url: str, username: str, password: str) -> bool:
target = login_url + quote(target_url, 'utf-8')
form = {
'username': username,
'password': password,
'execution': get_token(session, target),
'_eventId': 'submit',
'type': 'username_password',
'submit': "LOGIN"
}
response = session.post(target, data=form, allow_redirects=True)
soup = BeautifulSoup(response.text, 'html.parser')
return soup.text.find('综合评教系统') != -1