Why are you checking div for hello when hello is on the a element?
To clarify, you seem to be showing soup object, but then are operating on a testsoup object. It is very unclear as to what you are doing.
From my tests, things are working just fine:
from bs4 import BeautifulSoup soup = BeautifulSoup("<a test hello='1'>test</a>", "html.parser") print(soup.a.attrs) print(soup.encode(formatter="html5")) {'test': '', 'hello': '1'} b'<a hello="1" test>test</a>'Keep in mind that HTML using text, not boolean types. It is possible if you are using a literal boolean opposed to strings, that you are running into an issue. In the future, BeautifulSoup might coerce those to strings, but instead of waiting/relying on Beautiful Soup to do such things, it is better to get in the habit of using strings.
Probably something like
a.get('test') == ''It will get the attribute if available, and if not, it will return None. Then the value is then compared to the empty string.
Because that is how the parser represents them, it normalizes them. An attribute without a value has an empty value. You are showing one explicitly with an empty value, and one implicitly with an undefined value, but they are the same as far as the parser is concerned.
from bs4 import BeautifulSoup soup = BeautifulSoup("<a test hello>test</a>", "html.parser") print(soup.a.attrs) soup = BeautifulSoup("<a test hello=''>test</a>", "html.parser") print(soup.a.attrs) {'test': '', 'hello': ''} {'test': '', 'hello': ''}It should be noted that in a browser, an empty string value is no different to no value. While they are treated intuitively as boolean, they aren’t really booleans.
You can use this CSS in a browser [test=""] and find attributes with explicit and implicit attributes. There is no difference, and parsers should really have differences in this regards.