tags:

views:

50

answers:

1

How can i check valid of html code with python? i need closed tags check, and braces in tags params. Such as |a href="xxx'| and other possible validations, wich libs i can use for this?

A: 

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
Peter Lyons