Back to articles
Cheerio.js Cheat Sheet: Extract Data from Any HTML Page in 10 Lines

Cheerio.js Cheat Sheet: Extract Data from Any HTML Page in 10 Lines

via Dev.to TutorialАлексей Спинов

Cheerio is the fastest way to parse HTML in Node.js. jQuery syntax, zero browser overhead. Install npm install cheerio Basic Usage const cheerio = require ( ' cheerio ' ); const html = await fetch ( ' https://example.com ' ). then ( r => r . text ()); const $ = cheerio . load ( html ); // Extract data const title = $ ( ' h1 ' ). text (); const links = $ ( ' a ' ). map (( i , el ) => $ ( el ). attr ( ' href ' )). get (); const prices = $ ( ' .price ' ). map (( i , el ) => $ ( el ). text (). trim ()). get (); Common Patterns Extract Table Data const rows = $ ( ' table tr ' ). map (( i , row ) => { const cells = $ ( row ). find ( ' td ' ); return { name : $ ( cells [ 0 ]). text (). trim (), value : $ ( cells [ 1 ]). text (). trim () }; }). get (); Extract All Meta Tags const meta = {}; $ ( ' meta ' ). each (( i , el ) => { const name = $ ( el ). attr ( ' name ' ) || $ ( el ). attr ( ' property ' ); if ( name ) meta [ name ] = $ ( el ). attr ( ' content ' ); }); Extract Structured Data con

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
2 views

Related Articles