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
export type TableRow = {
[key: string]: string;
};
export async function parseCalendarEventsTable(html: string): Promise<TableRow[]> {
const { DOMParser } = await import(
"https://deno.land/x/deno_dom/deno-dom-wasm.ts"
);
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
const tables = doc.querySelectorAll("table");
const result: TableRow[][] = [];
tables.forEach((table) => {
let headers: string[] = [];
const rows: TableRow[] = [];
table.querySelectorAll("thead tr th").forEach((header) => {
headers.push(header.textContent.trim());
});
headers = headers.filter((h) => h !== "Data pager").filter((h) => h !== "");
table.querySelectorAll("tbody tr").forEach((row) => {
const rowData: TableRow = {};
row.querySelectorAll("td").forEach((cell, index) => {
const content = cell.textContent?.trim() || "";
if (content === "") return;
if (!headers[index]) return;
rowData[headers[index]] = cell.textContent.trim();
});
rows.push(rowData);
});
result.push(rows);
});
const eventsTable = result[5]; //the council calendar table is the 5th table found on the page
return result[5].slice(2); // the actual events start after the first two rows
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
February 25, 2024