How do I get a title in Cheerio?

You can get a title in Cheerio by using the title as the selector expression and then executing the text() method. Here is some sample code that extracts and prints the title from the ScrapingBee homepage:

const cheerio = require('cheerio');

fetch('https://scrapingbee.com')
    .then(function (response) {
        return response.text();
    })
    .then(function (html) {
        // Load HTML in Cheerio
        const $ = cheerio.load(html);
        
        // Use `title` as a selector and extract
        // the text using the `text()` method
        console.log($('title').text())
    })
    .catch(function (err) {
        console.log('Failed to fetch page: ', err);
    });

Related Cheerio web scraping questions: