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
// Scrapes Deno Blog and returns a JSON response with post titles, images, and URLs.
import cheerio from "npm:cheerio@1.0.0-rc.12";
export default async function main(req: Request): Promise<Response> {
const url = "https://deno.com/blog";
const response = await fetch(url);
const html = await response.text();
const $ = cheerio.load(html);
const blogPosts = [];
$("a[href^=\"/blog/\"]").each((index, element) => {
const $element = $(element);
const title = $element.find("h3").text().trim();
const url = $element.attr("href");
const img = $element.find("img").attr("src");
if (title && url && !url.includes("#")) {
blogPosts.push({
title,
url,
img,
});
}
});
return new Response(JSON.stringify(blogPosts, null, 2));
}