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
import { fetchText } from "https://esm.town/v/stevekrouse/fetchText?v=6";
import { load } from "npm:cheerio";
const html = await fetchText(
"https://www.gov.uk/government/publications/migrants-detected-crossing-the-english-channel-in-small-boats/migrants-detected-crossing-the-english-channel-in-small-boats-last-7-days",
);
const rawHtml = load(html);
const table = rawHtml("table").first();
const rows = table.find("tr");
let jsonTable = [];
// Iterate through each row
rows.each((i, row) => {
const columns = rawHtml(row).find("td, th");
let rowData = {};
// Iterate through each column
columns.each((j, column) => {
const columnText = rawHtml(column).text().trim();
// Use the first row to define the keys
if (i === 0) {
rowData[`column${j}`] = columnText;
} else {
rowData[jsonTable[0][`column${j}`]] = columnText;
}
});
// Add the row data to the table if it's not the header row
if (i !== 0) {
jsonTable.push(rowData);
} else {
// Add the header row as the first element
jsonTable.push(rowData);
}
});
jsonTable.shift();
console.log(jsonTable);
export default async function(req: Request): Promise<Response> {
return new Response(JSON.stringify(jsonTable), {
headers: { "Content-Type": "application/json" },
});
}