Validate an HTML fragment using html5lib

…衆ロ難τιáo~ 提交于 2019-12-11 10:38:40

问题


I'm using Python and html5lib to check if a bit of HTML code entered on a form field is valid.

I tried the following code to test a valid fragment but I'm getting an unexpected error (at least for me):

>>> import html5lib
>>> from html5lib.filters import lint
>>> fragment = html5lib.parseFragment('<p><script>alert("Boo!")</script></p>')
>>> walker = html5lib.getTreeWalker('etree')
>>> [i for i in lint.Filter(walker(fragment))]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/xyz/html5lib-1.0b3-py2.7.egg/html5lib/filters/lint.py", line 28, in __iter__
    raise LintError(_("Tag name is not a string: %(tag)r") % {"tag": name})
LintError: Tag name is not a string: u'p'

What I'm doing wrong?

My default encoding is utf-8:

>>> import sys
>>> sys.getdefaultencoding()
'utf-8'

回答1:


The lint filter doesn't attempt to validate HTML (uh, yeah, documentation is needed, badly… this is a large part of the reason there is no 1.0 release yet), it merely validates that the treewalker API is adhered to. Except it doesn't because it's broken because of issue #172.

html5lib doesn't attempt to provide any validator, as it's a lot of work to implement an HTML validator.

I'm unaware of any reasonably complete validator except for Validator.nu, though that is written in Java. It provides a web API which might be suitable for your purposes, however.



来源:https://stackoverflow.com/questions/29567776/validate-an-html-fragment-using-html5lib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!