检查 html 标签是否自关闭 - HTMLparser - Python [英] Check if html tag is self-closing - HTMLparser - Python
问题描述
有没有办法用 HTMLparser 来检查标签是否是自闭合标签?
Is there a way to check if a tag is a self-closing tag with HTMLparser?
我知道自闭标签是由内置函数处理的:handle_startendtag()
I know self-closing tags are handled by the built-in function: handle_startendtag()
然而,它只有在它们被明确关闭时才处理它们.例如 <img src="x.jpg"/>
However, it only handles them if they are explicitely closed..eg <img src="x.jpg"/>
而不是:<img src="x.jpg">
我正在制作一个程序,它接收一个 html 文件并输出一个 sass 模板.
I am making a program that takes an html file and spits out a sass template.
我想在输出文件中关闭这些在 html 文件中没有明确关闭的 img 标签.
I want to close these img tags in the output file that are not explicitly closed in the html file.
干杯
推荐答案
不完全是 Python 特定的解决方案,但是如果您想知道哪些 标签具有这种自关闭属性",您可以可以查看官方的 HTML5 规范:这些规范的正式名称为 空元素.
Not exactly a Python-specific solution, but if you want to know which tags have this "self-closing property", you can look at the official HTML5 specs: these are formally known as void elements.
area, base, br, col, embed, hr, img, input, keygen, link, menuitem,
meta, param, source, track, wbr
严格来说,void 元素根本没有结束标签,但是 在 >
之前允许额外的 /
.
Strictly speaking, void elements do not have closing tags at all, but permit an extra /
immediately before the >
.
这篇关于检查 html 标签是否自关闭 - HTMLparser - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!