如何使用 Node.js 解析 HTML 页面 [英] How do I parse a HTML page with Node.js

查看:79
本文介绍了如何使用 Node.js 解析 HTML 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析(服务器端)大量的 HTML 页面.
我们都同意 regexp 不适合这里.
在我看来,javascript 是解析 HTML 页面的本机方式,但这种假设依赖于具有 javascript 在浏览器中的所有 DOM 能力的服务器端代码.

I need to parse (server side) big amounts of HTML pages.
We all agree that regexp is not the way to go here.
It seems to me that javascript is the native way of parsing a HTML page, but that assumption relies on the server side code having all the DOM ability javascript has inside a browser.

Node.js 是否内置了这种能力?
有没有更好的办法解决这个问题,在服务器端解析HTML?

Does Node.js have that ability built in?
Is there a better approach to this problem, parsing HTML on the server side?

推荐答案

您可以使用 npm 模块 jsdomhtmlparser 在 Node.JS 中创建和解析 DOM.

You can use the npm modules jsdom and htmlparser to create and parse a DOM in Node.JS.

其他选项包括:

  • BeautifulSoup for python
  • you can convert you html to xhtml and use XSLT
  • HTMLAgilityPack for .NET
  • CsQuery for .NET (my new favorite)
  • The spidermonkey and rhino JS engines have native E4X support. This may be useful, only if you convert your html to xhtml.

在所有这些选项中,我更喜欢使用 Node.js 选项,因为它使用标准的 W3C DOM 访问器方法,并且我可以在客户端和服务器上重用代码.我希望 BeautifulSoup 的方法更类似于 W3C dom,而且我认为将您的 HTML 转换为 XHTML 来编写 XSLT 只是简单的虐待狂.

Out of all these options, I prefer using the Node.js option, because it uses the standard W3C DOM accessor methods and I can reuse code on both the client and server. I wish BeautifulSoup's methods were more similar to the W3C dom, and I think converting your HTML to XHTML to write XSLT is just plain sadistic.

这篇关于如何使用 Node.js 解析 HTML 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆