在 Python 中验证 (X)HTML [英] Validate (X)HTML in Python
问题描述
验证文档是否遵循某个 HTML 版本(最好是我可以指定的版本)的最佳方法是什么?我希望能够知道故障发生在哪里,就像在基于 Web 的验证器中一样,但在本机 Python 应用程序中除外.
What's the best way to go about validating that a document follows some version of HTML (prefereably that I can specify)? I'd like to be able to know where the failures occur, as in a web-based validator, except in a native Python app.
推荐答案
XHTML 很简单,使用 lxml.
XHTML is easy, use lxml.
from lxml import etree
from StringIO import StringIO
etree.parse(StringIO(html), etree.HTMLParser(recover=False))
HTML 更难,因为传统上 HTML 人群对验证没有那么大的兴趣(通过验证器运行 StackOverflow 本身,是的).最简单的解决方案是执行外部应用程序,例如 nsgmls 或 OpenJade,然后解析它们的输出.
HTML is harder, since there's traditionally not been as much interest in validation among the HTML crowd (run StackOverflow itself through a validator, yikes). The easiest solution would be to execute external applications such as nsgmls or OpenJade, and then parse their output.
这篇关于在 Python 中验证 (X)HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!