Last active
August 17, 2016 21:45
-
-
Save Phate334/91c1249fdb91e2a2108a to your computer and use it in GitHub Desktop.
登入高大elearning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from requests import Session | |
from bs4 import BeautifulSoup | |
class People(): | |
def __init__(self, id, password): | |
self.session = Session() | |
self.post_account(id, password) | |
def get(self, url): | |
res = self.session.get(url) | |
res.encoding = "utf-8" | |
return res.text.encode("utf-8") | |
def click(self, url): | |
self.session.get(url) | |
class Teacher(People): | |
def post_account(self, tid, password): | |
""" | |
post account data to login | |
id: teacher or ta school id. | |
password: password | |
""" | |
post_data = { | |
"logintype": "tea", | |
"seturl": "http://elearning.nuk.edu.tw/", | |
"CHKID": "8745", | |
"bTea_id": tid, | |
"bTea_pw": password | |
} | |
self.session.get("http://elearning.nuk.edu.tw/default.php") # get PHPSESSID in cookie | |
self.session.get("http://elearning.nuk.edu.tw/login_page_1.php") | |
self.session.post("http://elearning.nuk.edu.tw/tealogin_chk.php", data=post_data) | |
class Student(People): | |
def post_account(self, sid, password): | |
""" | |
post account data to login | |
id: teacher or ta school id. | |
password: password | |
""" | |
post_data = { | |
"logintype": "stu", | |
"seturl": "http://elearning.nuk.edu.tw/", | |
"CHKID": "9587", | |
"bTea_id": sid, | |
"bTea_pw": password | |
} | |
self.session.get("http://elearning.nuk.edu.tw/default.php") # get PHPSESSID in cookie | |
self.session.get("http://elearning.nuk.edu.tw/login_page_2.php") | |
self.session.post("http://stu.nuk.edu.tw/GEC/login_at2.asp", data=post_data) | |
self.session.get("http://elearning.nuk.edu.tw/login_chk.asp", params={"SID":sid,"CHKID":"9587"}) | |
self.session.get("http://elearning.nuk.edu.tw/p_login_stu_at.php", params={"bStu_id":sid}) | |
if __name__ == "__main__": | |
s = Student("學號", "密碼") | |
with open("st.html","w") as f: | |
f.write(s.get("http://elearning.nuk.edu.tw/m_student/m_stu_index.php")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
import json | |
from bs4 import BeautifulSoup | |
from logger import Student | |
class StdParser(): | |
"""用來解析學生elearning首頁 | |
""" | |
def __init__(self, index_html): | |
self.home_bs = BeautifulSoup(index_html, "lxml") | |
def get_name(self): | |
"""取得學生姓名 | |
Returns: | |
str. 學生姓名 | |
""" | |
return self.home_bs.find("font", color="#000066").text[:-2] | |
def get_semester(self): | |
"""取得當前年份與學期 | |
Returns: | |
tuple. (year, semester) | |
""" | |
semester_str = self.home_bs.find("font", color="#FF0000").text | |
year = semester_str[:3] | |
semester = semester_str[-3] | |
return (year, semester) | |
def get_first_list(self): | |
"""取出第一頁的課程列表 | |
Returns: | |
list. 每個元素都是一筆課程資料的字典物件 | |
""" | |
table = self.home_bs.find(id="myTable") | |
heads = [th.text for th in table.thead.find_all("th")] | |
class_list = [] | |
for tr in table.tbody.find_all("tr"): | |
tds = tr.find_all("td") | |
class_info = {heads[i]: td.text for i, td in enumerate(tds)} | |
class_info[u"url"] = tds[1].a["href"][2:] | |
class_list.append(class_info) | |
return class_list | |
def get_last_course_list(self): | |
"""取出最新一學期的課程 | |
Returns: | |
list. 每個元素都是一筆課程資料的字典物件 | |
""" | |
year, semester = self.get_semester() | |
class_list = self.get_first_list() | |
return [c for c in class_list if c[u"學年"]==year and c[u"學期"]==semester] | |
def get_time(self, class_info): | |
""" 解析上課時間資料 | |
Args: | |
class_info (dict): 從課程列表中取出的其中一個字典物件 | |
Returns: | |
tuple. (星期數, [時間]) | |
""" | |
time_info = class_info[u"上課時間"].split(u"_") | |
week = time_info[0][1] | |
times = time_info[1:] | |
return (week, times) | |
if __name__ == "__main__": | |
with open("st.html") as f: | |
page = f.read() | |
std = StdParser(page) | |
for t in std.get_last_course_list(): | |
print(json.dumps(t, ensure_ascii=False).encode("utf-8")) | |
print(std.get_time(std.get_last_course_list()[0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment