-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-json.js
52 lines (44 loc) · 1.4 KB
/
create-json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const fs = require("fs")
const fse = require("fs-extra")
const jsdom = require("jsdom")
const { JSDOM } = jsdom
const htmlJSON = fse.readFileSync("./index.html")
const dom = new JSDOM(htmlJSON, { includeNodeLocations: true })
const document = dom.window.document
const bodyEl = document.body // implicitly created
console.log(bodyEl.querySelectorAll("div").length)
const divs = bodyEl.querySelectorAll("div")
divs.forEach((el, index) => {
const title = el.querySelector("h1")
const auth = el.querySelector("h2")
const type = el.querySelector("h3")
const content = el.querySelector("content")
const desc = el.querySelector("desc")
const json = {
title: trim(title.innerHTML),
auth: trim(auth.innerHTML),
type: trim(type.innerHTML),
content: getContent(trim(content.innerHTML)),
// text: trim(content.innerHTML),
desc: trim(desc.innerHTML),
}
if (content.innerHTML.includes("strong")) {
console.log(json)
}
fs.writeFileSync(`./json/${index + 1}.json`, JSON.stringify(json, null, 2))
// console.log(json)
})
// 去除前后空格 和换行符
function trim(str = "") {
return str.replace(/(^\s*)|(\s*$)/g, "").replace(/\n/g, "")
}
function getContent(str = "") {
return str
.replace(/<br>/g, ",")
.replace(/\。/g, ",")
.replace(/\!/g, "")
.split(",")
.filter(
(item) => item && trim(item.replace(/<p>/g, "").replace(/<\/p>/g, ""))
)
}