-
Notifications
You must be signed in to change notification settings - Fork 0
Webシラバスからデータをスクレイピングして整形 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
7194148
172f08a
59ace7b
5d66a83
623c763
dabdaf4
b24fe31
c239721
93a3e34
abbecd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import requests as req | ||
| from bs4 import BeautifulSoup | ||
| from tkinter import * | ||
| from tkinter import ttk | ||
| import time | ||
|
|
||
| FETCH_SLEEP = 0 | ||
| room = ["特別教室", "階段教室", "ネットワーク情報センター", "第1講義室", "第2講義室", "第3講義室", "第4講義室", | ||
| "第5講義室", "第6講義室", "専攻科講義室A", "専攻科講義室B", | ||
| "専攻科ゼミ室A", "共通ゼミ室A", "マルチメディア講義室A", "マルチメディア講義室B", "多目的室A,B(講義棟C)", | ||
| "M製図室", "E実験室", "D実験室", | ||
| "実習工場", "J計算機演習室", "J回路実験室", "コミュニティールームB", "コミュミニティルームD", "CAD室", | ||
| "校内外フィールド", "C都市創造実験室・レクチャー室", "水理実験室", | ||
| "図書館フリー閲覧室", "学友会研修室1", "学友会館研修室2/3", "学友会館研修室4", "物理実験室", "化学実験室", | ||
| "ものづくり実習室", ""] | ||
| departmentName = [ | ||
| { | ||
| "name": "機械工学科", | ||
| "alphabet": 'M' | ||
| }, | ||
| { | ||
| "name": "電気電子工学科", | ||
| "alphabet": 'E' | ||
| }, | ||
| { | ||
| "name": "電子制御工学科", | ||
| "alphabet": 'D' | ||
| }, | ||
| { | ||
| "name": "情報工学科", | ||
| "alphabet": 'J' | ||
| }, | ||
| { | ||
| "name": "環境都市工学科", | ||
| "alphabet": 'C' | ||
| } | ||
| ] | ||
| subjectData: list[list[dict]] = [[], [], [], [], []] | ||
|
|
||
|
|
||
| def update_room(idx, select): | ||
| subjectData[idx][0]["room"] = select | ||
|
|
||
|
|
||
| def generation_progress_bar(num): | ||
| result = "[" | ||
| done = int(30 * num) | ||
| for count in range(done): | ||
| result += "#" | ||
| for count in range(30 - done): | ||
| result += "-" | ||
| result += "] " | ||
|
|
||
| if done == 30: | ||
| result += "done" | ||
| else: | ||
| result += str(num * 100 * 100 // 1 / 100) + "%" | ||
| return result | ||
|
|
||
|
|
||
| print("------------------------------") | ||
| print("KMM-timetable-crawler v0.0.0\ndeveloped by kokastar") | ||
| print("------------------------------") | ||
|
|
||
| print("年度を指定してください", end=":") | ||
| year: str = input() | ||
|
|
||
| subjectElements = [] | ||
| allSubjectSiz = 0 | ||
| print("\nfetching syllabus department data...") | ||
| print(generation_progress_bar(0), end="") | ||
| for i in range(5): | ||
| page = req.get("https://syllabus.kosen-k.go.jp/Pages/PublicSubjects?school_id=14&department_id=1" + str( | ||
| i + 1) + "&year=" + year + "&lang=ja") | ||
| soup = BeautifulSoup(page.text, "html.parser") | ||
| mcc_show = soup.find_all(class_="mcc-show") | ||
| subjectElements.append(mcc_show) | ||
| allSubjectSiz += len(mcc_show) | ||
| print("\r" + generation_progress_bar((i + 1) / 5), end="") | ||
| time.sleep(FETCH_SLEEP) | ||
|
|
||
| fetchedSubject = 0 | ||
| print("\nfetching subjects data...") | ||
| print(generation_progress_bar(0), end="") | ||
| for i in range(5): | ||
| for j in range(len(subjectElements[i])): | ||
| try: | ||
| url = "https://syllabus.kosen-k.go.jp" + subjectElements[i][j].attrs['href'] | ||
| page = req.get(url) | ||
| soup = BeautifulSoup(page.text, "html.parser") | ||
| tdData = soup.find_all("td") | ||
| subjectName = soup.find("h1").string | ||
| teachers = [] | ||
| for k in soup.find_all(id="Teachers"): | ||
| teachers.append(k.string) | ||
| subjectData[i].append({"subjectName": subjectName, "subjectId": str(i + 1) + tdData[10].string, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| "professor": teachers, "url": url, "room": ""}) | ||
| except KeyError: | ||
| a = 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pythonでは何もしない命令として
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あ、そこ消し忘れてた |
||
| # subjectPageData.append() | ||
| fetchedSubject += 1 | ||
| print("\r" + generation_progress_bar(fetchedSubject / allSubjectSiz), end="") | ||
| time.sleep(FETCH_SLEEP) | ||
|
|
||
| for i in range(len(subjectData)): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 機械工学科1年~5年の国語IAの授業の教室の場所しか選択できない |
||
| grade = int(subjectData[i][0]["subjectId"]) // 10 | ||
| department = int(subjectData[i][0]["subjectId"]) % 10 - 1 | ||
|
Comment on lines
+106
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'grade'と'department'おそらく逆 |
||
| className = str(grade) + "年" + departmentName[department]["name"] + "教室" | ||
| room[len(room) - 1] = className | ||
| update_room(i, className) | ||
|
|
||
| root = Tk() | ||
| root.title(className + " " + subjectData[i][0]["subjectName"]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 重要度低めです。 |
||
| # Frame | ||
| frame = ttk.Frame(root, padding=10) | ||
| frame.grid() | ||
| # Combobox | ||
| v = StringVar() | ||
| cb = ttk.Combobox( | ||
| frame, textvariable=v, | ||
| values=room, width=30, ) | ||
| cb.set(room[len(room) - 1]) | ||
| cb.grid(row=0, column=0) | ||
| # Button | ||
| button1 = ttk.Button( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OKボタンを押したときでも、閉じるボタンを押した時みたいにwindowが消える仕様であって欲しい
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 僕は嫌なので別で実装してる(取り消すと長時間のスクレイピングをもう一度せざるを得ないので) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see |
||
| frame, text='OK', | ||
| command=lambda: update_room(i, v.get())) | ||
| button2 = ttk.Button( | ||
| frame, text='close', | ||
| command=root.destroy) | ||
| button1.grid(row=0, column=1) | ||
| button2.grid(row=0, column=2) | ||
| root.mainloop() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 慣例的に
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. それは僕がコピペしてきたサイトに言ってください...
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. まぁ確かにrequirementsのほうが自然ではある |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| beautifulsoup4==4.12.2 | ||
| certifi==2023.7.22 | ||
| charset-normalizer==3.3.0 | ||
| idna==3.4 | ||
| requests==2.31.0 | ||
| soupsieve==2.5 | ||
| urllib3==2.0.6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
出力結果を見るとroomがほぼすべて空になってます