Repository navigation
Expand file tree
/
Copy pathscrape_test.js
More file actions
76 lines (66 loc) · 2.7 KB
/
Copy pathscrape_test.js
File metadata and controls
76 lines (66 loc) · 2.7 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
const fs = require("fs");
const path = require("path");
const https = require("https");
const http = require("http");
const cheerio = require("cheerio");
const baseUrl = "https://www.csgraduates.com";
function fetchUrl(url) {
return new Promise((resolve, reject) => {
const client = url.startsWith("https") ? https : http;
client.get(url, { timeout: 30000 }, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
const redirectUrl = res.headers.location.startsWith("http")
? res.headers.location : new URL(res.headers.location, url).href;
return fetchUrl(redirectUrl).then(resolve).catch(reject);
}
if (res.statusCode !== 200) { reject(new Error("HTTP " + res.statusCode)); return; }
let data = "";
res.on("data", chunk => data += chunk);
res.on("end", () => resolve(data));
}).on("error", reject).on("timeout", function() { this.destroy(); reject(new Error("Timeout")); });
});
}
async function test() {
const year = 2023;
const html = await fetchUrl(`${baseUrl}/study_methods/408quiz/${year}/`);
const $ = cheerio.load(html);
// Debug: count h5 elements
console.log("H5 count:", $("h5").length);
// Debug: check first few h5
$("h5").slice(0, 5).each((i, el) => {
console.log("H5[" + i + "]:", $(el).text().trim(), " | id:", $(el).attr("id"));
});
// Debug: check answer table
console.log("\nAnswer table rows:", $(".elegant-detail-content table tbody tr").length);
const answerMap = {};
$(".elegant-detail-content table tbody tr").each((_, row) => {
const cells = $(row).find("td");
for (let i = 0; i < cells.length; i += 2) {
const num = $(cells[i]).text().trim();
const ans = $(cells[i + 1]).text().trim();
if (num && ans) answerMap[num] = ans;
}
});
console.log("Answer map size:", Object.keys(answerMap).length);
// Debug: check choice containers
console.log("\nChoice containers:", $(".choice-container").length);
// Debug: check first h5 and its next siblings
const firstH5 = $("h5").first();
console.log("\nFirst h5 text:", firstH5.text().trim());
let next = firstH5.next();
let count = 0;
while (next.length > 0 && count < 5) {
const tag = (next.prop("tagName") || "").toLowerCase();
const cls = next.attr("class") || "";
const text = next.text().trim().substring(0, 100);
console.log(` Next sibling: <${tag}> class="${cls}" text="${text}"`);
next = next.next();
count++;
}
// Debug: check h4 headers
console.log("\nH4 headers:");
$("h4").each((i, el) => console.log(" " + $(el).text().trim()));
// Check SVGs
console.log("\nSVG wrappers:", $(".svg-wrapper").length);
}
test().catch(console.error);