Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, August 22, 2016

How do I parse a HTML page with Node.js

How do I parse a HTML page with Node.js


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.

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

Answer by kzh for How do I parse a HTML page with Node.js


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

Other options include:

  • 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.

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.

Quick example for jsdom:

var jsdom = require("jsdom");  jsdom.env({      file: 'some file.html',      done: function (err, window) {          GLOBAL.window = window;          GLOBAL.document = window.document;          // now you can work on parsing HTML as you normally would in a browser          // e.g. this will work            showTables();      }  });  function showTables() {      var tables = document.querySelectorAll('table');      console.log("there are ", tables.length, " tables").  }  

Answer by josh3736 for How do I parse a HTML page with Node.js


In .NET, there's the HTML Agility Pack, which is an extremely solid HTML parsing library.

Answer by esp for How do I parse a HTML page with Node.js


Htmlparser2 by FB55 seems to be a good alternative.

Answer by Yarek T for How do I parse a HTML page with Node.js


jsdom is too strict to do any real screen scraping sort of things, but beautifulsoup doesn't choke on bad markup.

node-soupselect is a port of python's beautifulsoup into nodejs, and it works beautifully

Answer by Meekohi for How do I parse a HTML page with Node.js


Use Cheerio. It isn't as strict as jsdom and is optimized for scraping. As a bonus, uses the jQuery selectors you already know.

? Familiar syntax: Cheerio implements a subset of core jQuery. Cheerio removes all the DOM inconsistencies and browser cruft from the jQuery library, revealing its truly gorgeous API.

? Blazingly fast: Cheerio works with a very simple, consistent DOM model. As a result parsing, manipulating, and rendering are incredibly efficient. Preliminary end-to-end benchmarks suggest that cheerio is about 8x faster than JSDOM.

? Insanely flexible: Cheerio wraps around @FB55's forgiving htmlparser. Cheerio can parse nearly any HTML or XML document.

Answer by Anderson Madeira for How do I parse a HTML page with Node.js


Use htmlparser2, its way faster and pretty straightforward. Consult this usage example:

https://www.npmjs.org/package/htmlparser2#usage

And the live demo here:

http://demos.forbeslindesay.co.uk/htmlparser2/


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.