Created
October 2, 2018 07:32
-
-
Save azu/5b092cafcd7d569fb38a6bdb06f94e5b to your computer and use it in GitHub Desktop.
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
const { JSDOM } = require("jsdom"); | |
const dom = new JSDOM(``, { | |
url: "http://www.gsi.go.jp/KOKUJYOHO/MOUNTAIN/mountain.html", | |
contentType: "text/html", | |
includeNodeLocations: true, | |
storageQuota: 10000000 | |
}); | |
function tableToJson(table) { | |
const data = []; // first row needs to be headers var headers = []; | |
const headers = []; | |
for (let i = 0; i < table.rows[0].cells.length; i++) { | |
headers[i] = table.rows[0].cells[i].textContent.trim(); | |
} | |
// go through cells | |
for (let i = 1; i < table.rows.length; i++) { | |
const tableRow = table.rows[i]; | |
const rowData = {}; | |
for (let j = 0; j < tableRow.cells.length; j++) { | |
const rowName = headers[j]; | |
const tableCell = tableRow.cells[j]; | |
if (rowName === "山名<山頂名>") { | |
rowData["山名(ふりがな)"] = tableCell.querySelector("small").textContent.trim(); | |
rowData["山名"] = tableCell.querySelector("a").textContent.trim(); | |
} else { | |
rowData[rowName] = tableCell.textContent.trim(); | |
} | |
} | |
data.push(rowData); | |
} | |
return data; | |
} | |
const table = dom.window.document.querySelector("table"); | |
const json = tableToJson(table); | |
console.log(json); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment