-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraping.js
More file actions
130 lines (111 loc) · 3.79 KB
/
scraping.js
File metadata and controls
130 lines (111 loc) · 3.79 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const axios = require("axios");
const fs = require("fs").promises;
const cheerio = require("cheerio");
async function numbersToFiles(week, problems) {
await Promise.all(
problems.map(async (item) => {
let limit_item = [];
let limit_content = [];
let title;
let description;
let input;
let output;
let sampleCount = 0;
let sampleInputs = [];
let sampleOutputs = [];
try {
const headers = {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
};
const { data } = await axios.get(
`https://www.acmicpc.net/problem/${item}`,
{
headers: headers,
}
);
let $ = cheerio.load(data);
title = $("#problem_title").text();
$("#problem-info")
.find("th")
.each((idx, item) => {
limit_item.push($(item).text().trim());
});
$("#problem-info")
.find("td")
.each((idx, item) => {
limit_content.push($(item).text().trim());
});
description = $("#problem_description").find("p").text().trim();
input = $("#problem_input").find("p").text().trim();
output = $("#problem_output").find("p").text().trim();
while (1) {
if (
$(`#sampleinput${sampleCount + 1}`)
.find("h2")
.text() !== ""
) {
let input = $(`#sampleinput${sampleCount + 1}`)
.find("pre")
.text();
let output = $(`#sampleoutput${sampleCount + 1}`)
.find("pre")
.text();
sampleInputs.push(input);
sampleOutputs.push(output);
sampleCount++;
} else {
break;
}
}
let result = `# boj_${item} ${title}
<a href="https://www.acmicpc.net/problem/${item}">문제 바로 가기</a>
| **${limit_item[0]}** | **${limit_item[1]}** | **${limit_item[2]}** | **${limit_item[3]}** | **${limit_item[4]}** | **${limit_item[5]}** |
| ------------- | --------------- | -------- | -------- | ------------- | ------------- |
| **${limit_content[0]}** | **${limit_content[1]}** | **${limit_content[2]}** | **${limit_content[3]}** | **${limit_content[4]}** | **${limit_content[5]}** |
### 문제
${description}
### 입력
${input}
### 출력
${output}
`;
let inputAndOutput = "";
for (let i = 0; i < sampleCount; i++) {
inputAndOutput =
inputAndOutput +
`### 예제 입력${i + 1}\n\n\`\`\`\n${
sampleInputs[i]
}\`\`\`\n\n### 예제 출력${i + 1}\n\n\`\`\`\n${
sampleOutputs[i]
}\`\`\`\n\n`;
}
result = result + inputAndOutput;
try {
await fs.mkdir(`week${week}/boj_${item}(${title})/${item}_sy`, {
recursive: true,
});
await fs.mkdir(`week${week}/boj_${item}(${title})/${item}_doin`, {
recursive: true,
});
await fs.mkdir(`week${week}/boj_${item}(${title})/${item}_sun`, {
recursive: true,
});
await fs.mkdir(`week${week}/boj_${item}(${title})/${item}_uk`, {
recursive: true,
});
await fs.writeFile(
`week${week}/boj_${item}(${title})/problem.md`,
result
);
console.log(`File for problem ${item} created successfully.`);
} catch (e) {
console.error(`Error creating file for problem ${item}:`, e);
}
} catch (e) {
console.error(`Error processing problem ${item}:`, e);
}
})
);
}
numbersToFiles(8, [14502, 19237, 21609, 16926, 2920, 15903]);