Node.js下有哪些解析Html代码的框架?

Node.js下有哪些解析Html代码的框架?

Node.js下有哪些解析Html代码的框架? 目前知道的就是cheerio和node-jquery。 大家说说呗……

7 回复

Node.js 下有哪些解析 HTML 代码的框架?

在 Node.js 中解析 HTML 代码是一个常见的需求,无论是用于数据抓取、网页渲染还是其他用途。以下是几个流行的 HTML 解析框架,它们各有特点和适用场景。

1. Cheerio

Cheerio 是一个非常轻量级的库,它的语法与 jQuery 非常相似,这使得它易于上手。它主要用于解析和操作 HTML 文档,非常适合需要快速处理 HTML 的场景。

安装

npm install cheerio

示例代码

const cheerio = require('cheerio');
const html = `
  <html>
    <head><title>Example Page</title></head>
    <body>
      <div class="content">
        <h1>Hello, World!</h1>
        <p>This is an example page.</p>
      </div>
    </body>
  </html>
`;

const $ = cheerio.load(html);
const title = $('title').text();
const content = $('.content h1').text();

console.log(title); // 输出: Example Page
console.log(content); // 输出: Hello, World!

2. jsdom

jsdom 是一个完整的 Web 浏览器环境的实现,可以在 Node.js 环境中使用。它可以加载整个 HTML 文档,并提供对 DOM 和 BOM 的完全访问。虽然功能强大,但相比 Cheerio 而言,jsdom 更重一些。

安装

npm install jsdom

示例代码

const { JSDOM } = require("jsdom");

const html = `
  <html>
    <head><title>Example Page</title></head>
    <body>
      <div class="content">
        <h1>Hello, World!</h1>
        <p>This is an example page.</p>
      </div>
    </body>
  </html>
`;

const dom = new JSDOM(html);
const title = dom.window.document.querySelector('title').textContent;
const content = dom.window.document.querySelector('.content h1').textContent;

console.log(title); // 输出: Example Page
console.log(content); // 输出: Hello, World!

3. parse5

parse5 是一个快速且兼容性良好的 HTML5 解析器和 DOM 操作库。它专注于性能和标准兼容性,适合需要高性能解析的场景。

安装

npm install parse5

示例代码

const parse5 = require('parse5');
const html = `
  <html>
    <head><title>Example Page</title></head>
    <body>
      <div class="content">
        <h1>Hello, World!</h1>
        <p>This is an example page.</p>
      </div>
    </body>
  </html>
`;

const document = parse5.parse(html);
const title = document.querySelector('title').childNodes[0].value;
const content = document.querySelector('.content h1').childNodes[0].value;

console.log(title); // 输出: Example Page
console.log(content); // 输出: Hello, World!

以上是几个常用的 Node.js HTML 解析框架,根据不同的需求选择合适的工具可以大大提高开发效率。


框架也是用来解决生产中需要解决的问题的,如果cheerio和node-jquery都满足不了楼主的需求, 楼主可以自己造轮子啊。:)

谢谢啊。

哪个大神移植下BeautifulSoup啊,那个库好用啊

推荐cheerio

node下的HTML环境,不光是解析HTML,按需求来了, jsdom:https://github.com/tmpvar/jsdom

Node.js 下解析 HTML 代码的框架除了 cheerio 和 node-jquery 外,还有许多其他优秀的库。以下是一些常见的选择:

  1. Cheerio Cheerio 是一个轻量级的 HTML 解析器,它的 API 设计灵感来源于 jQuery,非常适合服务器端解析 HTML。

    示例代码:

    const cheerio = require('cheerio');
    const html = `
      <html>
        <head><title>Example</title></head>
        <body>
          <div class="content">Hello, world!</div>
        </body>
      </html>
    `;
    
    const $ = cheerio.load(html);
    const content = $('div.content').text();
    console.log(content); // 输出: Hello, world!
    
  2. JSDOM JSDOM 是一个纯 JavaScript 实现的浏览器 DOM 模拟器,可以在 Node.js 中使用它来解析和操作 HTML 文档。

    示例代码:

    const { JSDOM } = require('jsdom');
    
    const html = `
      <html>
        <head><title>Example</title></head>
        <body>
          <div id="main">Welcome to JSDOM!</div>
        </body>
      </html>
    `;
    
    const dom = new JSDOM(html);
    const mainContent = dom.window.document.querySelector('#main').textContent;
    console.log(mainContent); // 输出: Welcome to JSDOM!
    
  3. parse5 parse5 是一个高性能的 HTML5 解析器,专为 Node.js 环境设计,可以用于处理复杂的 HTML 文档。

    示例代码:

    const parse5 = require('parse5');
    const html = `
      <html>
        <head><title>Example</title></head>
        <body>
          <div class="main">Parse5 is awesome!</div>
        </body>
      </html>
    `;
    
    const ast = parse5.parse(html);
    const divs = parse5.treeAdapters.default.querySelectorAll(ast, '.main');
    console.log(parse5.treeAdapters.default.getTextContent(divs[0])); // 输出: Parse5 is awesome!
    

这些框架各有特点,可以根据具体需求选择合适的工具。

回到顶部