python检查html有效
问题描述:
如何使用python检查html代码的有效性? 我需要关闭标签检查,并在标签参数中使用大括号.例如| a href ="xxx'|以及其他可能的验证,我可以使用这些文件来实现此目的?
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?
答
好吧,这并不是您要找的东西,但是要验证我工作的网站的HTML,我请W3C验证程序验证为我检查一下,然后我将屏幕抓取输出以得到基本的是/否结果.请注意,网络上有多种验证服务可以替代,但是W3C对我来说足够好用.
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