32

What HTML parsers have the following features:

  • Fast
  • Thread-safe
  • Reliable and bug-free
  • Parses HTML and XML
  • Handles erroneous HTML
  • Has a DOM implementation
  • Supports HTML4, JavaScript, and CSS tags
  • Relatively simple, object-oriented API

What parser you think is better?

Thank you.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Shayan
  • 2,758
  • 7
  • 36
  • 55

7 Answers7

16

Check out Web Harvest. It's both a library you can use and a data extraction tool, which sounds to me that's exactly what you want to do. You create XML script files to instruct the scraper how to extract the information you need and from where. The provided GUI is very useful to quickly test the scripts.

Check out the project's samples page to see if it's a good fit for what you are trying to do.

Cesar
  • 5,488
  • 2
  • 29
  • 36
7

The best known are NekoHTML and JTidy.

NekoHTML is based on Xerces, and provides a simple adaptable SAXParser which implements XMLReader JavaSE interface.

JTidy is more intented into formatting your html code into something XML-valid, but is still very useful as an XML parser, producing a DOM tree if needed.

You could have a look at this list for other alternatives.

Another choice could be to use hpricot through jRuby.

Valentin Rocher
  • 11,667
  • 45
  • 59
6

Validator.nu's HTML parser, definitely. It's an implementation of the HTML5 parsing algorithm, and Gecko is in the process of replacing its own HTML parser with a C++ translation of this one.

Ms2ger
  • 15,596
  • 6
  • 36
  • 35
5

Apache Tika is the best choice. Apache has recently extracted many sub-projects out of the existing projects and made them public. Tika is one of them that was previously a component of Apache Lucene. Because of Apache's support and reputation and the widely-used parent project Lucene it must be a very good choice. Furthermore, it is open-source.

A brief introduction from Apache Tika web site:

The Apache Tika™ toolkit detects and extracts metadata and structured text content from various documents using existing parser libraries.

And the supported formats are:

HyperText Markup Language
XML and derived formats
Microsoft Office document formats
OpenDocument Format
Portable Document Format
Electronic Publication Format
Rich Text Format
Compression and packaging formats
Text formats
Audio formats
Image formats
Video formats
Java class files and archives
The mbox format
reevesy
  • 3,452
  • 1
  • 26
  • 23
Shayan
  • 2,758
  • 7
  • 36
  • 55
  • 1
    Apache Tika is an excellent suggestion. Even if you are not interested in reading XML/HTML/MS DOC formats you can just specify "text/plain". It will stream in the data so it doesn't need to preload the whole file first. List of benefits: http://tika.apache.org/1.4/parser.html Article with sample code: http://www.openlogic.com/wazi/bid/314389/Content-mining-with-Apache-Tika – Salvador Valencia Dec 03 '13 at 21:48
  • 1
    I came for a solid HTML Parser, and left with one that I won't have to spend the time to generalize. I love this game. – Inversus Jan 18 '14 at 09:00
1

Well:

there aren't so many good HTML parsers in java as you need, but here are some alternatives: http://java-source.net/open-source/html-parsers

Very few of them support Javascript. Actually, I think you'll have to do this part on your own using Rhino (http://www.mozilla.org/rhino/).

Kico Lobo
  • 4,374
  • 4
  • 35
  • 48
1

I think that HTML Cleaner is what you're looking for. See its announcement on TheServerSide to see how it compare to JTidy, TagSoup, NekoHtml.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • But this is also for transforming into well XMLs. My main goal is extracting data from it. – Shayan Jan 24 '10 at 23:53
  • @Shayan So what? Doesn't it allow you to extract data from it? Doesn't it offer DOM manipulation? Doesn't it allow to parse nasty HTML? I don't get you. – Pascal Thivent Jan 25 '10 at 01:00
1

you probably want to look at doing something like running Mozilla in headless mode. Here is a link to get you started, I am sure you can use Google to find out more information.