Cheerio Parsing

作者 mindrally97184105b5da无许可证269 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5周前更新

Expert guidance for HTML/XML parsing using Cheerio in Node.js with best practices for DOM traversal, data extraction, and efficient scraping pipelines.

AI 生成的概览

指导在 Node.js 中使用 Cheerio 解析 HTML/XML,涵盖 DOM 遍历、数据提取与抓取流程。

功能
该技能提供在 Node.js 中使用 Cheerio 解析 HTML 和 XML 的专家指导与代码模式。内容涵盖文档加载、CSS 选择器、DOM 遍历、文本与属性提取、表格与列表提取、URL 解析、缺失数据处理以及性能优化建议。还包含一个结合 Cheerio 与 HTTP 客户端的完整抓取示例。
适用场景
适用于编写或审查解析 HTML/XML、从页面提取结构化数据或构建网页抓取流程的 Node.js 代码。也适合优化选择器效率以及处理格式错误的标记或缺失元素。
运行要求
需要 Node.js 及 cheerio 包;获取页面时涉及 axios、got 或 node-fetch 等 HTTP 客户端,以及用于并发控制的 p-limit。仅在抓取远程页面时需要网络访问。该技能不附带脚本,仅为说明与代码示例。

Cheerio HTML Parsing

You are an expert in Cheerio, Node.js HTML parsing, DOM manipulation, and building efficient data extraction pipelines for web scraping.

Core Expertise

  • Cheerio API and jQuery-like syntax
  • CSS selector optimization
  • DOM traversal and manipulation
  • HTML/XML parsing strategies
  • Integration with HTTP clients (axios, got, node-fetch)
  • Memory-efficient processing of large documents
  • Data extraction patterns and best practices

Key Principles

  • Write clean, modular extraction functions
  • Use efficient selectors to minimize parsing overhead
  • Handle malformed HTML gracefully
  • Implement proper error handling for missing elements
  • Design reusable scraping utilities
  • Follow functional programming patterns where appropriate

Basic Setup

bash
npm install cheerio axios

Loading HTML

javascript
const cheerio = require('cheerio');const axios = require('axios');
// Load from stringconst $ = cheerio.load('<html><body><h1>Hello</h1></body></html>');
// Load with optionsconst $ = cheerio.load(html, {  xmlMode: false,           // Parse as XML  decodeEntities: true,     // Decode HTML entities  lowerCaseTags: false,     // Keep tag case  lowerCaseAttributeNames: false});
// Fetch and parseasync function fetchAndParse(url) {  const response = await axios.get(url);  return cheerio.load(response.data);}

Selecting Elements

CSS Selectors

javascript
// By tag$('h1')
// By class$('.article')
// By ID$('#main-content')
// By attribute$('[data-id="123"]')$('a[href^="https://"]')  // Starts with$('a[href$=".pdf"]')      // Ends with$('a[href*="example"]')   // Contains
// Combinations$('div.article > h2')     // Direct child$('div.article h2')       // Any descendant$('h2 + p')               // Adjacent sibling$('h2 ~ p')               // General sibling
// Pseudo-selectors$('li:first-child')$('li:last-child')$('li:nth-child(2)')$('li:nth-child(odd)')$('tr:even')$('input:not([type="hidden"])')$('p:contains("specific text")')

Multiple Selectors

javascript
// Select multiple types$('h1, h2, h3')
// Chain selections$('.article').find('.title')

Extracting Data

Text Content

javascript
// Get text (includes child text)const text = $('h1').text();
// Get trimmed textconst text = $('h1').text().trim();
// Get HTMLconst html = $('div.content').html();
// Get outer HTMLconst outerHtml = $.html($('div.content'));

Attributes

javascript
// Get attributeconst href = $('a').attr('href');const src = $('img').attr('src');
// Get data attributesconst id = $('div').data('id');  // data-id attribute
// Check if attribute existsconst hasClass = $('div').hasClass('active');

Multiple Elements

javascript
// Iterate with eachconst items = [];$('.product').each((index, element) => {  items.push({    name: $(element).find('.name').text().trim(),    price: $(element).find('.price').text().trim(),    url: $(element).find('a').attr('href')  });});
// Map to arrayconst titles = $('h2').map((i, el) => $(el).text()).get();
// Filter elementsconst featured = $('.product').filter('.featured');
// First/Lastconst first = $('li').first();const last = $('li').last();
// Get by indexconst third = $('li').eq(2);

DOM Traversal

Navigation

javascript
// Parent$('span').parent()$('span').parents()          // All ancestors$('span').parents('.container')  // Specific ancestor$('span').closest('.wrapper')    // Nearest ancestor matching selector
// Children$('ul').children()           // Direct children$('ul').children('li.active') // Filtered children$('div').contents()          // Including text nodes
// Siblings$('li').siblings()$('li').next()$('li').nextAll()$('li').prev()$('li').prevAll()

Filtering

javascript
// Filter by selector$('li').filter('.active')
// Filter by function$('li').filter((i, el) => $(el).data('price') > 100)
// Find within selection$('.article').find('img')
// Check conditions$('li').is('.active')  // Returns boolean$('li').has('span')    // Has descendant matching selector

Data Extraction Patterns

Table Extraction

javascript
function extractTable(tableSelector) {  const $ = this;  const headers = [];  const rows = [];
  // Get headers  $(tableSelector).find('th').each((i, el) => {    headers.push($(el).text().trim());  });
  // Get rows  $(tableSelector).find('tbody tr').each((i, row) => {    const rowData = {};    $(row).find('td').each((j, cell) => {      rowData[headers[j]] = $(cell).text().trim();    });    rows.push(rowData);  });
  return rows;}

List Extraction

javascript
function extractList(selector, itemExtractor) {  return $(selector).map((i, el) => itemExtractor($(el))).get();}
// Usageconst products = extractList('.product', ($el) => ({  name: $el.find('.name').text().trim(),  price: parseFloat($el.find('.price').text().replace('$', '')),  image: $el.find('img').attr('src'),  link: $el.find('a').attr('href')}));

Pagination Links

javascript
function extractPaginationLinks() {  return $('.pagination a')    .map((i, el) => $(el).attr('href'))    .get()    .filter(href => href && !href.includes('#'));}

Handling Missing Data

javascript
// Safe extraction with defaultsfunction safeText(selector, defaultValue = '') {  const el = $(selector);  return el.length ? el.text().trim() : defaultValue;}
function safeAttr(selector, attr, defaultValue = null) {  const el = $(selector);  return el.length ? el.attr(attr) : defaultValue;}
// Optional chaining patternconst price = $('.price').first().text()?.trim() || 'N/A';

URL Resolution

javascript
const { URL } = require('url');
function resolveUrl(baseUrl, relativeUrl) {  if (!relativeUrl) return null;  try {    return new URL(relativeUrl, baseUrl).href;  } catch {    return relativeUrl;  }}
// Usageconst baseUrl = 'https://example.com/products/';$('a').each((i, el) => {  const href = $(el).attr('href');  const absoluteUrl = resolveUrl(baseUrl, href);  console.log(absoluteUrl);});

Performance Optimization

javascript
// Cache selectionsconst $products = $('.product');$products.each((i, el) => {  const $product = $(el);  // Wrap once  // Use $product multiple times});
// Limit parsing scopeconst $article = $('.article');const title = $article.find('.title').text();  // Searches only within article
// Use specific selectors// Good$('div.product > h2.title')// Less efficient$('div').find('.product').find('h2').filter('.title')

Complete Scraping Example

javascript
const cheerio = require('cheerio');const axios = require('axios');
async function scrapeProducts(url) {  const response = await axios.get(url, {    headers: {      'User-Agent': 'Mozilla/5.0 (compatible; MyScraper/1.0)'    }  });
  const $ = cheerio.load(response.data);  const products = [];
  $('.product-card').each((index, element) => {    const $el = $(element);
    products.push({      name: $el.find('.product-title').text().trim(),      price: parseFloat(        $el.find('.price').text().replace(/[^0-9.]/g, '')      ),      rating: parseFloat($el.find('.rating').attr('data-rating')) || null,      image: $el.find('img').attr('src'),      url: new URL($el.find('a').attr('href'), url).href,      inStock: !$el.find('.out-of-stock').length    });  });
  return products;}
// With error handlingasync function safeScrape(url) {  try {    return await scrapeProducts(url);  } catch (error) {    console.error(`Failed to scrape ${url}:`, error.message);    return [];  }}

Key Dependencies

  • cheerio
  • axios (HTTP client)
  • got (alternative HTTP client)
  • node-fetch (fetch API for Node.js)
  • p-limit (concurrency control)

Best Practices

  1. Always handle missing elements gracefully
  2. Use specific selectors for better performance
  3. Cache jQuery-wrapped elements when reusing
  4. Normalize extracted text (trim whitespace)
  5. Resolve relative URLs to absolute
  6. Validate extracted data types
  7. Implement rate limiting when scraping multiple pages
  8. Use appropriate User-Agent headers
  9. Handle character encoding issues
  10. Log extraction failures for debugging

来源与署名

来源:mindrally/skills位于cheerio-parsing提交9718410

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架