html5-phpvshtml5-parser
HTML5 is a standards-compliant HTML5 parser and writer written entirely in PHP. It is stable and used in many production websites, and has well over five million downloads.
HTML5 provides the following features:
- An HTML5 serializer
- Support for PHP namespaces
- Composer support
- Event-based (SAX-like) parser
- A DOM tree builder
- Interoperability with QueryPath
- Runs on PHP 5.3.0 or newer
Note that html5-php is a low-level HTML parser and does not feature any query features like CSS selectors.
html5-parser is a Python library for parsing HTML and XML documents.
A fast implementation of the HTML 5 parsing spec for Python. Parsing is done in C using a variant of the gumbo parser. The gumbo parse tree is then transformed into an lxml tree, also in C, yielding parse times that can be a thirtieth of the html5lib parse times. That is a speedup of 30x. This differs, for instance, from the gumbo python bindings, where the initial parsing is done in C but the transformation into the final tree is done in python.
It is built on top of the popular lxml library and provides a simple and intuitive API for working with the document's structure.
html5-parser uses the HTML5 parsing algorithm, which is more lenient and forgiving than the traditional XML-based parsing algorithm. This means that it can parse HTML documents with malformed or missing tags and still produce a usable parse tree.
To use html5-parser, you first need to install it via pip by running pip install html5-parser
.
Once it is installed, you can use the html5_parser.parse() function to parse an HTML document and create a parse tree. For example:
from html5_parser import parse
html_string = "<html><body>Hello, World!</body></html>"
root = parse(html_string)
print(root.tag) # html
Once you have a parse tree, you can use the find()
and findall()
methods to search for elements
in the document similar to BeautifulSoup.
html5-parser also supports searching using xpath, similar to lxml.
Example Use
<?php
// Assuming you installed from Composer:
require "vendor/autoload.php";
use Masterminds\HTML5;
// An example HTML document:
$html = <<< 'HERE'
<html>
<head>
<title>TEST</title>
</head>
<body id='foo'>
<h1>Hello World</h1>
<p>This is a test of the HTML5 parser.</p>
</body>
</html>
HERE;
// Parse the document. $dom is a DOMDocument.
$html5 = new HTML5();
$dom = $html5->loadHTML($html);
// Render it as HTML5:
print $html5->saveHTML($dom);
// Or save it to a file:
$html5->save($dom, 'out.html');
from html5_parser import parse
html_string = "<html><body>Hello, World!</body></html>"
root = parse(html_string)
print(root.tag) # html
body = root.find("body")
# or find all
print(body.text) # "Hello, World!"
for el in root.findall("p"):
print(el.text) # "Hello