$ node syllabus.js 2021 14
puppeteerが必要です。引数についてはソースコードを参照してください。
// | |
const fs = require("fs") | |
const path = require("path") | |
const puppeteer = require("puppeteer") | |
async function getSyllabus(browser, schoolId, departmentId, year, lang) { | |
const page = await browser.newPage() | |
const url = `https://syllabus.kosen-k.go.jp/Pages/PublicSubjects?school_id=${schoolId}&department_id=${departmentId}&year=${year}&lang=${lang}` | |
await page.goto(url) | |
return await page.evaluate(() => { | |
const trs = document.querySelectorAll("table#sytablenc > tbody tr") | |
const syllabus = Array.from(trs).map((tr) => { | |
if (tr.className === "ctop") return null | |
const row = Array.from(tr.querySelectorAll("td")).map( | |
(x) => x.innerText || "" | |
) | |
if (row === undefined || (row[0] !== "一般" && row[0] !== "専門")) | |
return null | |
const general = row[0] === "一般" | |
const required = row[1] === "必修" | |
const title = row[2] | |
const id = parseInt(row[3], 10) | |
const gakushu = row[4] === "学修単位" | |
const credit = parseInt(row[5], 10) | |
const classes = row.slice(6, 26).flatMap((val, i) => { | |
if (i % 2 == 0) { | |
return val.length === 0 ? 0 : parseInt(val, 10) | |
} else { | |
return [] | |
} | |
}) | |
const lecturer = row[26] | |
const notes = row[27] // 履修上の区分 | |
if (title.includes("留学生") || title.includes("海外研修")) return null | |
return { | |
general, | |
required, | |
title, | |
id, | |
gakushu, | |
credit, | |
classes, | |
lecturer, | |
notes, | |
} | |
}) | |
return syllabus.filter((x) => x !== null) | |
}) | |
} | |
function save(filename, object) { | |
console.log(`Saving to ${filename}...`) | |
const json = JSON.stringify(object, null, " ") | |
fs.mkdir(path.dirname(filename), { recursive: true }, (err) => { | |
if (err) console.log(err) | |
}) | |
fs.writeFile(filename, json, (err, result) => { | |
if (err) console.log(err) | |
}) | |
} | |
!(async () => { | |
try { | |
const browser = await puppeteer.launch() | |
const schoolId = 27 // 明石高専 | |
// 14 for EJ, 13 for ED, 12 for E, 11 for M, 15 for C, 16 for A | |
// 21 for ME, 22 for AC | |
const departmentId = process.argv[3] | |
const year = process.argv[2] | |
const lang = "ja" | |
const syllabus = await getSyllabus( | |
browser, | |
schoolId, | |
departmentId, | |
year, | |
lang | |
) | |
console.log(syllabus) | |
const path = `./syllabus/${schoolId}_${departmentId}_${year}_${lang}.json` | |
save(path, syllabus) | |
browser.close() | |
} catch (e) { | |
console.error(e) | |
} | |
})() |