ARTICLE AD BOX
As you stated, the order of attributes in HTML doesn't matter. But if you really want unsorted attributes, you can do:
from bs4 import BeautifulSoup from bs4.formatter import HTMLFormatter class UnsortedAttributes(HTMLFormatter): def attributes(self, tag): yield from tag.attrs.items() tags = BeautifulSoup('<span id="100" class="test"></span>', "html.parser") print(tags.encode(formatter=UnsortedAttributes()).decode())Prints:
<span id="100" class="test"></span>EDIT: To not close void tags you can try:
class UnsortedAttributes(HTMLFormatter): def __init__(self): super().__init__( void_element_close_prefix="" ) # <-- use void_element_close_prefix="" here def attributes(self, tag): yield from tag.attrs.items() tags = BeautifulSoup( """<input id="NOT_CLOSED_TAG" type="Button">""", "html.parser", ) print(tags.encode(formatter=UnsortedAttributes()).decode())Prints:
<input id="NOT_CLOSED_TAG" type="Button">4 Comments
@Minions I can only point you how bs4 handles special HTML tags (after all, it's HTML/XML parser): git.launchpad.net/beautifulsoup/tree/bs4/builder/… You can subclass TreeBuilder and use custom empty_element_tags, but this is more work...
2023-07-31T23:17:27.637Z+00:00
Explore related questions
See similar questions with these tags.


