Is there a way to check if a tag is a self-closing tag with HTMLparser?
I know self-closing tags are handled by the built-in function: handle_startendtag()
However, it only handles them if they are explicitely closed..eg <img src="x.jpg"/>
and not: <img src="x.jpg">
I am making a program that takes an html file and spits out a sass template.
I want to close these img tags in the output file that are not explicitly closed in the html file.
Cheers
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.
Strictly speaking, void elements do not have closing tags at all, but permit an extra
/
immediately before the>
.Simple solution is to use BeautifulSoup.
You can also check if a tag is self closing or not.
Every
Tag
element hasisSelfClosing
property. So, you can filter them out.