- Services
- Case Studies
- Technologies
- NextJs development
- Flutter development
- NodeJs development
- ReactJs development
- About
- Contact
- Tools
- Blogs
- FAQ
Parse HTML with Cheerio in Node.js Guide
This guide covers installation, basic usage, advanced techniques, and best practices for web scraping and content extraction.
How to Parse HTML with Cheerio in Node.js

Web scraping and HTML parsing are essential skills in a developer’s toolkit. Whether you’re building a data aggregator, creating a content monitoring system, or automating data extraction, knowing how to effectively parse HTML is crucial. Today, let’s dive into Cheerio, a fast and lightweight library that brings jQuery-like syntax to server-side HTML manipulation in Node.js.
What is Cheerio?
Think of Cheerio as your Swiss Army knife for HTML parsing in Node.js. It’s like jQuery for the server - familiar, powerful, and incredibly efficient. Unlike heavy-duty browsers or DOM implementations, Cheerio is designed to be blazing fast and memory-efficient, making it perfect for parsing large HTML documents.

Getting Started with Cheerio
First things first, let’s set up our project. Open your terminal and create a new project directory. Then, initialize your Node.js project and install Cheerio:
mkdir cheerio-tutorialcd cheerio-tutorialnpm init -ynpm install cheerio axiosNow, let’s write a simple script that demonstrates Cheerio’s power. Here’s a basic example that fetches and parses a webpage:
const cheerio = require('cheerio');const axios = require('axios');
async function scrapeWebsite() { try { // Fetch HTML content const response = await axios.get('https://example.com'); const html = response.data;
// Load HTML into Cheerio const $ = cheerio.load(html);
// Select and extract data const pageTitle = $('h1').text(); const paragraphs = $('p').map((i, el) => $(el).text()).get();
console.log('Page Title:', pageTitle); console.log('Paragraphs:', paragraphs); } catch (error) { console.error('Error:', error); }}
scrapeWebsite();Advanced Cheerio Techniques
Let’s explore some more powerful features that make Cheerio truly shine:
Selecting Elements
Cheerio supports various jQuery-like selectors:
// Select by ID$('#mainContent');
// Select by class$('.article-body');
// Select by attribute$('a[href^="https"]');
// Combining selectors$('div.content > p.important');
Traversing the DOM
Navigate through HTML elements with ease:
// Find child elements$('article').children();
// Find parent elements$('p').parent();
// Find siblings$('h2').siblings();
// Find specific elements$('div').find('span');Manipulating Elements
While Cheerio is primarily used for parsing, it can also modify HTML:
// Add a class$('div').addClass('new-class');
// Set attributes$('img').attr('alt', 'Description');
// Modify text content$('p').text('New text content');Best Practices and Tips
- Always handle errors appropriately
- Use specific selectors to improve performance
- Cache your Cheerio instance when parsing large documents
- Remember to respect websites’ robots.txt and rate limiting
- Consider using async/await for cleaner code
Conclusion
Cheerio is an incredibly powerful tool for HTML parsing in Node.js. Its familiar jQuery-like syntax, combined with Node.js’s efficiency, makes it an excellent choice for web scraping and HTML manipulation tasks. Whether you’re building a simple scraper or a complex data extraction system, Cheerio’s simplicity and performance make it a go-to choice for developers.

สร้างเว็บไซต์ 1 เว็บ ต้องใช้งบเท่าไหร่? เจาะลึกทุกองค์ประกอบ website development cost อยากสร้างเว็บไซต์แต่ไม่มั่นใจในเรื่องของงบประมาณ อ่านสรุปเจาะลึกตั้งแต่ดีไซน์, ฟังก์ชัน และการดูแล พร้อมตัวอย่างงบจริงจาก Till it’s done ที่แผนชัด งบไม่บานปลายแน่นอน
Next.js สอน 14 ขั้นตอนเบื้องต้น: สร้างโปรเจกต์แรกใน 30 นาที เริ่มต้นกับ Next.js ใน 14 ขั้นตอนเพียงแค่ 30 นาที พร้อม SSR/SSG และ API Routes ด้วยตัวอย่างโค้ดง่าย ๆ อ่านต่อเพื่อสร้างโปรเจ็กต์แรกได้ทันทีที่นี่
วิธีสมัคร Apple Developer Account เพื่อนำแอปขึ้น App Store ทีละขั้นตอน อยากปล่อยแอปบน App Store ระดับโลก มาอ่านคู่มือสมัคร Apple Developer Account พร้อมเคล็ดลับ TestFlight และวิธีอัปโหลดที่ง่ายในบทความเดียวนี้ได้เลย
TypeScript Interface คืออะไร? อธิบายพร้อมวิธีใช้และข้อแตกต่างจาก Type เรียนรู้วิธีใช้ TypeScript Interface เพื่อสร้างโครงสร้างข้อมูลที่ปลอดภัยและเข้าใจง่าย พร้อมเปรียบเทียบข้อดีข้อแตกต่างกับ Type ที่คุณต้องรู้ ถูกรวมเอาไว้ในบทความนี้แล้ว
Material-UI (MUI) คืออะไร อยากสร้าง UI สวยงามและเป็นมืออาชีพในเวลาอันรวดเร็วใช่ไหม มาทำความรู้จักกับ Material-UI (MUI) ที่ช่วยให้คุณพัฒนาแอปพลิเคชันบน React ได้ง่ายและดูดีในทุกอุปกรณ์
เปรียบเทียบ 3 วิธีติดตั้ง install node js บน Ubuntu: NVM vs NodeSource vs Official Repo แบบไหนดีที่สุด? เรียนรู้วิธีติดตั้ง Node.js บน Ubuntu ด้วย NVM, NodeSource หรือ Official Repo เลือกวิธีที่เหมาะกับความต้องการของคุณ พร้อมเปรียบเทียบ เพื่อการพัฒนาที่มีประสิทธิภาพ! พูดคุยกับซีอีโอ
We'll be right here with you every step of the way.
We'll be here, prepared to commence this promising collaboration.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.