Repository navigation
Expand file tree
/
Copy pathscrape.js
More file actions
214 lines (164 loc) · 6.28 KB
/
Copy pathscrape.js
File metadata and controls
214 lines (164 loc) · 6.28 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import puppeteer from "puppeteer";
import fsPromises from "fs/promises";
// SAFETY: Helper function to pause execution and prevent IP bans
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function scrapeLeetcodeProblems() {
const browser = await puppeteer.launch({
headless: false, // Change to "new" when moving to a backend server
defaultViewport: null,
args: ["--disable-blink-features=AutomationControlled"],
});
const page = await browser.newPage();
await page.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/114.0.5735.199 Safari/537.36"
);
await page.goto("https://leetcode.com/problemset/", {
waitUntil: "domcontentloaded",
});
const problemSelector =
"a.group.flex.flex-col.rounded-\\[8px\\].duration-300";
let allProblems = [];
let prevCount = 0;
const TARGET = 1000;
console.log("Scrolling LeetCode to fetch 1000 problem links...");
while (allProblems.length < TARGET) {
await page.evaluate((sel) => {
const currProblemsOnPage = document.querySelectorAll(sel);
if (currProblemsOnPage.length) {
currProblemsOnPage[currProblemsOnPage.length - 1].scrollIntoView({
behavior: "smooth",
block: "end",
});
}
}, problemSelector);
await delay(2000);
try {
await page.waitForFunction(
(sel, prev) => document.querySelectorAll(sel).length > prev,
{ timeout: 5000 },
problemSelector,
prevCount
);
} catch(e) {
console.log("No more problems loading or reached the bottom.");
break;
}
allProblems = await page.evaluate((sel) => {
const nodes = Array.from(document.querySelectorAll(sel));
return nodes.map((el) => ({
title: el
.querySelector(".ellipsis.line-clamp-1")
?.textContent.trim()
.split(". ")[1],
url: el.href,
}));
}, problemSelector);
prevCount = allProblems.length;
process.stdout.write(`\rFound ${allProblems.length} links...`);
}
console.log(`\nStarting to scrape ${Math.min(TARGET, allProblems.length)} LeetCode descriptions...`);
const problemsWithDescriptions = [];
const loopLimit = Math.min(allProblems.length, TARGET);
for (let i = 0; i < loopLimit; i++) {
const { title, url } = allProblems[i];
const problemPage = await browser.newPage();
try {
await problemPage.goto(url);
await delay(Math.floor(Math.random() * 1500) + 1500);
let description = await problemPage.evaluate(() => {
const descriptionDiv = document.querySelector(
'div.elfjS[data-track-load="description_content"]'
);
if (!descriptionDiv) return "Description not found";
const paragraphs = descriptionDiv.querySelectorAll("p");
let collectedDescription = [];
for (const p of paragraphs) {
if (p.innerHTML.trim() === " ") break;
collectedDescription.push(p.innerText.trim());
}
return collectedDescription.filter((text) => text !== "").join(" ");
});
problemsWithDescriptions.push({ title, url, description });
process.stdout.write(".");
} catch (err) {
console.error(`\nError fetching description for ${title} (${url}):`, err.message);
} finally {
await problemPage.close();
}
}
await fsPromises.mkdir("./problems", { recursive: true });
// RESTORED: leetcode_problems.json
await fsPromises.writeFile(
"./problems/leetcode_problems.json",
JSON.stringify(problemsWithDescriptions, null, 2)
);
await browser.close();
console.log("\n✅ LeetCode Scrape Complete!");
}
async function scrapeCodeforcesProblems() {
const browser = await puppeteer.launch({
headless: false, // Change to "new" when moving to a backend server
defaultViewport: null,
args: ["--disable-blink-features=AutomationControlled"],
});
const page = await browser.newPage();
await page.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/114.0.5735.199 Safari/537.36"
);
const problems = [];
const TARGET = 1000;
console.log("Starting Codeforces Scrape...");
for (let i = 1; problems.length < TARGET; i++) {
const url = `https://codeforces.com/problemset/page/${i}`;
await page.goto(url, { waitUntil: "domcontentloaded" });
const problemSelector =
"table.problems tr td:nth-of-type(2) > div:first-of-type > a";
const links = await page.evaluate((sel) => {
const anchors = document.querySelectorAll(sel);
return Array.from(anchors).map((a) => a.href);
}, problemSelector);
for (let j = 0; j < links.length && problems.length < TARGET; j++) {
const link = links[j];
try {
await page.goto(link, { waitUntil: "domcontentloaded" });
await delay(Math.floor(Math.random() * 1500) + 1500);
const { title, description } = await page.evaluate(() => {
const titleEl = document.querySelector(".problem-statement .title");
const descEl = document.querySelector(".problem-statement > div:nth-of-type(2)");
const title = titleEl ? titleEl.textContent.split(". ")[1] : "Unknown";
const description = descEl ? descEl.textContent : "No description found";
return { title, description };
});
problems.push({
title,
url: link,
description,
});
process.stdout.write(".");
} catch (err) {
console.warn(`\n❌ Failed to scrape ${link}: ${err.message}`);
}
}
}
await fsPromises.mkdir("./problems", { recursive: true });
// RESTORED: codeforces_problems.json
await fsPromises.writeFile(
"./problems/codeforces_problems.json",
JSON.stringify(problems, null, 2)
);
await browser.close();
console.log("\n✅ Codeforces Scrape Complete!");
}
async function runAll() {
console.log("🧹 Wiping old problem files clean...");
// Wipes the folder clean to guarantee no ghost files
await fsPromises.rm("./problems", { recursive: true, force: true }).catch(() => {});
await scrapeCodeforcesProblems();
await scrapeLeetcodeProblems();
console.log("🎉 All scraping finished successfully!");
}
runAll();