什么是验证文档是否遵循某些HTML版本(最好是我可以指定)的最佳方法?我希望能够知道故障发生的位置,就像在基于Web的验证器中一样,除了在本机Python应用程序中.
PyTidyLib是HTML Tidy的一个很好的python绑定.他们的例子:
from tidylib import tidy_document document, errors = tidy_document('''fõo ''', options={'numeric-entities':1}) print document print errors
此外,它兼容遗留的HTML Tidy和新的tidy-html5.
我认为它是最优雅的方式来调用W3C验证服务
http://validator.w3.org/
编程.很少有人知道你不必屏幕抓取结果才能得到结果,因为服务返回非标准的HTTP标头参数
X-W3C-Validator-Recursion: 1 X-W3C-Validator-Status: Invalid (or Valid) X-W3C-Validator-Errors: 6 X-W3C-Validator-Warnings: 0
用于指示错误和警告的有效性和数量.
例如,命令行
curl -I "http://validator.w3.org/check?uri=http%3A%2F%2Fwww.stalsoft.com"
回报
HTTP/1.1 200 OK Date: Wed, 09 May 2012 15:23:58 GMT Server: Apache/2.2.9 (Debian) mod_python/3.3.1 Python/2.5.2 Content-Language: en X-W3C-Validator-Recursion: 1 X-W3C-Validator-Status: Invalid X-W3C-Validator-Errors: 6 X-W3C-Validator-Warnings: 0 Content-Type: text/html; charset=UTF-8 Vary: Accept-Encoding Connection: close
因此,您可以优雅地调用W3C验证服务并从HTTP标头中提取结果:
# Programmatic XHTML Validations in Python
# Martin Hepp and Alex Stolz
# mhepp@computer.org / alex.stolz@ebusiness-unibw.org
import urllib
import urllib2
URL = "http://validator.w3.org/check?uri=%s"
SITE_URL = "http://www.heppnetz.de"
# pattern for HEAD request taken from
# http://stackoverflow.com/questions/4421170/python-head-request-with-urllib2
request = urllib2.Request(URL % urllib.quote(SITE_URL))
request.get_method = lambda : 'HEAD'
response = urllib2.urlopen(request)
valid = response.info().getheader('X-W3C-Validator-Status')
if valid == "Valid":
valid = True
else:
valid = False
errors = int(response.info().getheader('X-W3C-Validator-Errors'))
warnings = int(response.info().getheader('X-W3C-Validator-Warnings'))
print "Valid markup: %s (Errors: %i, Warnings: %i) " % (valid, errors, warnings)
XHTML很简单,使用lxml.
HTML更难,因为传统上对HTML人群的验证没有那么大的兴趣(通过验证器运行StackOverflow本身,yikes).最简单的解决方案是执行外部应用程序,如nsgmls或OpenJade,然后解析其输出.
您可以决定在本地安装HTML验证程序并创建客户端以请求验证.
在这里,我创建了一个程序来验证txt文件中的url列表.我只是检查HEAD以获得验证状态,但是如果你进行GET,你将获得完整的结果.查看验证器的API,有很多选项.
import httplib2 import time h = httplib2.Http(".cache") f = open("urllistfile.txt", "r") urllist = f.readlines() f.close() for url in urllist: # wait 10 seconds before the next request - be nice with the validator time.sleep(10) resp= {} url = url.strip() urlrequest = "http://qa-dev.w3.org/wmvs/HEAD/check?doctype=HTML5&uri="+url try: resp, content = h.request(urlrequest, "HEAD") if resp['x-w3c-validator-status'] == "Abort": print url, "FAIL" else: print url, resp['x-w3c-validator-status'], resp['x-w3c-validator-errors'], resp['x-w3c-validator-warnings'] except: pass
试试tidylib.您可以获得一些非常基本的绑定作为elementtidy模块的一部分(从HTML文档构建元素树).http://effbot.org/downloads/#elementtidy
>>> import _elementtidy >>> xhtml, log = _elementtidy.fixup("") >>> print log line 1 column 1 - Warning: missing declaration line 1 column 7 - Warning: discarding unexpected