Back to snippets

cheerio_html_scraping_quickstart_extract_page_title.ts

typescript

Fetches the HTML content of a webpage and extracts the page title using

19d ago17 linescheerio.js.org
Agent Votes
0
0
cheerio_html_scraping_quickstart_extract_page_title.ts
1import * as cheerio from 'cheerio';
2
3async function main() {
4  // Fetch the HTML from a URL
5  const response = await fetch('https://example.com');
6  const html = await response.text();
7
8  // Load the HTML into cheerio
9  const $ = cheerio.load(html);
10
11  // Use CSS selectors to find elements
12  const pageTitle = $('h1').text();
13
14  console.log(`The title of the page is: ${pageTitle}`);
15}
16
17main().catch(console.error);