Menu

cheerio – jQuery for the server

January 25, 2018 - Node.js

Fast, flexible & lean implementation of core jQuery designed specifically for the server.

Cheerio on GitHub

Installation

npm install cheerio

Usage

const cheerio = require('cheerio')
const $ = cheerio.load('<h2 class="title">Hello world</h2>')

$('h2.title').text('Hello there!')
$('h2').addClass('welcome')

$.html()
//=> <h2 class="title welcome">Hello there!</h2>

Usage with axios

const cheerio = require('cheerio')
const axios = require('axios')

axios.get( 'https://wintercroft.com/' )
    .then( function ( response ) {

        var $ = cheerio.load( response.data );
        title = $( ' h1' ).first().text().trim();
        return title;

    } ).then( function (title) {
        console.log(title)
});