64 lines
2.4 KiB
Python
64 lines
2.4 KiB
Python
from sys import argv
|
|
from requests import Session
|
|
from bs4 import BeautifulSoup
|
|
from requests.models import Response
|
|
|
|
class Universite:
|
|
def __init__(self, url, pseudo, motDePasse):
|
|
self.url = url
|
|
self.loginData = {
|
|
"username": pseudo,
|
|
"password": motDePasse,
|
|
"_eventId": "submit",
|
|
"submit": "SE CONNECTER"
|
|
}
|
|
|
|
def maSoupe(self, page):
|
|
return BeautifulSoup(page.content, "html.parser")
|
|
|
|
""" def ecrirePageHTML(self, texte): # Utilisé pour du DEBUG
|
|
with open("page.html", "w") as f:
|
|
f.write(texte) """
|
|
|
|
def recuperationNotes(self):
|
|
with Session() as session:
|
|
# login
|
|
reponse = session.get(self.url)
|
|
soup = self.maSoupe(reponse)
|
|
self.loginData["lt"] = soup.find("input", attrs = {"name": "lt"})["value"]
|
|
self.loginData["execution"] = soup.find("input", attrs = {"name": "execution"})["value"]
|
|
reponse = session.post(self.url, data = self.loginData)
|
|
|
|
# page des résultats intermédiaire
|
|
soup = self.maSoupe(reponse)
|
|
try:
|
|
url = soup.find("a", attrs = {"id": "service-407"})["href"]
|
|
except:
|
|
raise TimeoutError("Le site a prit trop de temps pour répondre, veuillez réessayez plus tard.")
|
|
reponse = session.get(url, allow_redirects = False)
|
|
url = reponse.headers["Location"]
|
|
reponse = session.get(url)
|
|
|
|
# page des notes
|
|
soup = self.maSoupe(reponse)
|
|
url = f"{url}?{soup.find('form', attrs = {'enctype': 'application/x-www-form-urlencoded'})['action'].split('?')[1].replace('welcome', 'detailnotes')}"
|
|
reponse = session.get(url)
|
|
# self.ecrirePageHTML(reponse.text)
|
|
|
|
# récupération tableaux des notes
|
|
soup = self.maSoupe(reponse)
|
|
for attrs in soup.findAll("table"):
|
|
try:
|
|
texte = str(attrs).split("thead")[1][2:-2]
|
|
while ' ' in texte:
|
|
texte = texte.replace(' ', ' ')
|
|
return texte
|
|
except:
|
|
pass
|
|
|
|
if __name__ == "__main__":
|
|
argv = argv[1:]
|
|
if len(argv) == 3:
|
|
print(Universite(*argv).recuperationNotes())
|
|
else:
|
|
print("Merci de renseigner l'URL, le pseudo et le mot de passe (avec des \").")
|