2

How can I check the validity of HTML code with Python?

I need closed tags check, and braces in tags params. Such as |a href="xxx'| and other possible validations, which libs I can use for this?

2 Answers 2

3

Well, this isn't exactly what you're looking for, but to validate the HTML for a web site I work on, I ask the W3C Validator to check it for me, and I just screen scrape the output to get the basic yes/no result. Note there are several validation services on the web as alternatives, but W3C works well enough for me.

#!/usr/bin/python2.6
import re
import urllib
import urllib2

def validate(URL):
    validatorURL = "http://validator.w3.org/check?uri=" + \
        urllib.quote_plus(URL)
    opener = urllib2.urlopen(validatorURL)
    output = opener.read()
    opener.close()
    if re.search("This document was successfully checked as".replace(
            " ", r"\s+"), output):
        print "    VALID: ", URL
    else:
        print "INVALID: ", URL
Sign up to request clarification or add additional context in comments.

1 Comment

stackoverflow.com/a/10519634/2729778 is a similar but cleaner answer.
2

The html5lib module can be used to perform basic HTML validation:

>>> import html5lib
>>> html5parser = html5lib.HTMLParser(strict=True)
>>> html5parser.parse('<html></html>')
Traceback (most recent call last):
  ...
html5lib.html5parser.ParseError: Unexpected start tag (html). Expected DOCTYPE.
>>> html5parser.parseFragment('<p>Lorem <a href="/foobar">ipsum</a>')
<Element 'DOCUMENT_FRAGMENT' at 0x7f1d4a58fd60>
>>> html5parser.parseFragment('<p>Lorem </a>ipsum<a href="/foobar">')
Traceback (most recent call last):
  ...
html5lib.html5parser.ParseError: Unexpected end tag (a). Ignored.
>>> html5parser.parseFragment('<p><form></form></p>')
Traceback (most recent call last):
  ...
html5lib.html5parser.ParseError: Unexpected end tag (p). Ignored.
>>> html5parser.parseFragment('<option value="example" />')
Traceback (most recent call last):
  ...
html5lib.html5parser.ParseError: Trailing solidus not allowed on element option

1 Comment

This seems not to catch the following invalid HTML: html5parser.parseFragment('<span><div></div></span>').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.